如何在项目中导入 Eclipse JDT 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/93728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:18:46  来源:igfitidea点击:

How can I import eclipse JDT classes in a project

eclipseeclipse-jdt

提问by user5915

I want to do the following imports in a class.

我想在一个类中进行以下导入。

import org.eclipse.jdt.core.dom.*;  
import org.eclipse.jdt.core.compiler.CharOperation;  
import org.eclipse.jdt.core.compiler.IProblem;  
import org.eclipse.jdt.internal.compiler.ClassFile;  
import org.eclipse.jdt.internal.compiler.CompilationResult;  
import org.eclipse.jdt.internal.compiler.Compiler;    
import org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies;  
import org.eclipse.jdt.internal.compiler.ICompilerRequestor;  
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;  
import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException;  
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;  
import org.eclipse.jdt.internal.compiler.env.INameEnvironment;  
import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;  
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;  
import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;  
import org.eclipse.jface.text.Document;  
import org.eclipse.jface.text.BadLocationException;  
import org.eclipse.text.edits.TextEdit;  

How can I import the JDT within Eclipse? Cheers.

如何在 Eclipse 中导入 JDT?干杯。

回答by delfuego

I think I found an easier way to do this:

我想我找到了一种更简单的方法来做到这一点:

  • right-click on your project in the Package Explorer;
  • choose "Build Path...";
  • choose "Configure Build Path";
  • choose the Libraries tab;
  • click the "Add Variable..." button;
  • in the list box, choose the "ECLIPSE_HOME" entry, and then click the "Extend" button;
  • in the list box, open up the "plugins" folder entry, scroll way down, and shift-click all the org.eclipse.jdt.* JAR files that are in the file listing beneaththe folders;
  • click OK until you're all the way back out.
  • 在包资源管理器中右键单击您的项目;
  • 选择“构建路径...”;
  • 选择“配置构建路径”;
  • 选择库选项卡;
  • 单击“添加变量...”按钮;
  • 在列表框中,选择“ECLIPSE_HOME”条目,然后单击“扩展”按钮;
  • 在列表框中,打开“plugins”文件夹条目,向下滚动,然后按住 Shift 键并单击文件夹下方文件列表的所有 org.eclipse.jdt.* JAR 文件;
  • 单击“确定”,直到您完全退出。

That should do it.

那应该这样做。

回答by delfuego

Unless I'm misunderstanding you, you just need to include the JDT JAR files on your classpath; they're all available in your Eclipse plugins directory. So for your project, right-click on the project's name in the Package Explorer, go to the Build Path... submenu, and choose Configure Build Path. Then in the Libraries tab, use the "Add External JARs" button to add each of the relevant JAR files from the Eclipse plugins directory.

除非我误解了您的意思,否则您只需要在类路径中包含 JDT JAR 文件;它们都可以在您的 Eclipse 插件目录中找到。因此,对于您的项目,在 Package Explorer 中右键单击项目名称,转到 Build Path... 子菜单,然后选择 Configure Build Path。然后在库选项卡中,使用“添加外部 JAR”按钮从 Eclipse 插件目录中添加每个相关的 JAR 文件。

回答by jamesh

If your'e writing plugins for Eclipse, you shouldn't really be trying to instantiate the internalpackages. According to this API Rules of Engagement

如果您为 Eclipse 编写插件,则您不应该真正尝试实例化internal包。根据此API 参与规则

Stick to officially documented APIs. Only reference packages that are documented in the published API Javadoc for the component. Never reference a package belonging to another component that has "internal" in its name---these are never API. Never reference a package for which there is no published API Javadoc---these are not API either.

坚持使用官方记录的 API。仅引用组件的已发布 API Javadoc 中记录的包。永远不要引用属于另一个名称中包含“内部”的组件的包——这些永远不是 API。永远不要引用没有发布 API Javadoc 的包——这些也不是 API。

For the others, add the package name to the Import-Packageentry in your manifest.

对于其他人,将包名称添加到Import-Package清单中的条目。

There are extension points into the JDT, but if what you want to do falls outside of these, then I'm afraid you're out of luck.

JDT 中有扩展点,但是如果您想做的不在这些范围内,那么恐怕您就不走运了。

If you're just looking to use a compiler in your code, without relying on the JDK (i.e. on a JRE), then I would consider shipping with a more standalone Java based Java compiler like Janino.

如果您只是想在代码中使用编译器,而不依赖于 JDK(即 JRE),那么我会考虑使用更独立的基于 Java 的 Java 编译器,如Janino

回答by Fabian Steeg

If you need these classes, you are probably in a plug-in project already. You should be able to import these classes by applying the quick fix "Fix project setup..." (Ctrl+1) on the line where Eclipse is complaining about the imports. That will add the required plug-ins to your MANIFEST.MF file in the META-INF directory (org.eclipse.jdt.core and org.eclipse.jface.text in your case). You can also add them manually in your MANIFEST.MF file. If your project is no plug-in project (and you have no MANIFEST.MF file) you can convert it by right-click on the project -> PDE Tools -> Convert Projects to Plug-in Project first. If you add dependencies to plug-in projects in the normal way ("configure build path") the classloading won't work properly at runtime (though it will compile).

如果您需要这些类,您可能已经在插件项目中了。您应该能够通过在 Eclipse 抱怨导入的行上应用快速修复“修复项目设置...”(Ctrl+1) 来导入这些类。这会将所需的插件添加到 META-INF 目录中的 MANIFEST.MF 文件(在您的情况下为 org.eclipse.jdt.core 和 org.eclipse.jface.text)。您还可以在 MANIFEST.MF 文件中手动添加它们。如果您的项目不是插件项目(并且您没有 MANIFEST.MF 文件),您可以先通过右键单击项目 -> PDE 工具 -> 将项目转换为插件项目来转换它。如果您以正常方式(“配置构建路径”)向插件项目添加依赖项,则类加载在运行时将无法正常工作(尽管它会编译)。