如何在项目中导入Eclipse JDT类

时间:2020-03-06 14:22:00  来源:igfitidea点击:

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

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;

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

解决方案

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

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

  • 在Package Explorer中右键单击项目;
  • 选择"构建路径...";
  • 选择"配置构建路径";
  • 选择库选项卡;
  • 点击"添加变量..."按钮;
  • 在列表框中,选择" ECLIPSE_HOME"条目,然后单击"扩展"按钮;
  • 在列表框中,打开"插件"文件夹条目,向下滚动,然后按住Shift并单击所有在文件夹下面的列表中的org.eclipse.jdt。* JAR文件;
  • 单击确定,直到完全退出。

那应该做。

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

如果我们正在为Eclipse编写插件,那么我们实际上不应该尝试实例化"内部"软件包。根据此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.

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

JDT中有扩展点,但是如果我们想做的事情不在这些范围之内,那么恐怕我们不走运。

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