如何在项目中导入eclipse JDT类

时间:2023-01-24 17:21:02

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?干杯。

4 个解决方案

#1


15  

I think I found an easier way to do this:

我想我找到了一种更简单的方法:

  • right-click on your project in the Package Explorer;
  • 在Package Explorer中右键单击您的项目;

  • choose "Build Path...";
  • 选择“Build Path ...”;

  • choose "Configure Build Path";
  • 选择“配置构建路径”;

  • choose the Libraries tab;
  • 选择Libraries选项卡;

  • click the "Add Variable..." button;
  • 单击“添加变量...”按钮;

  • in the list box, choose the "ECLIPSE_HOME" entry, and then click the "Extend" button;
  • 在列表框中,选择“ECLIPSE_HOME”条目,然后单击“扩展”按钮;

  • 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 beneath the folders;
  • 在列表框中,打开“plugins”文件夹条目,向下滚动,然后按住Shift键并单击文件夹下面的文件列表中的所有org.eclipse.jdt。* JAR文件;

  • click OK until you're all the way back out.
  • 单击确定,直到您完全退出。

That should do it.

应该这样做。

#2


3  

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。然后在Libraries选项卡中,使用“Add External JARs”按钮添加Eclipse插件目录中的每个相关JAR文件。

#3


2  

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

如果您正在为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.

坚持正式记录的API。仅引用已发布的API Javadoc中为该组件记录的包。永远不要引用属于其名称中具有“内部”的其他组件的包 - 这些包绝不是API。永远不要引用没有已发布的API Javadoc的包 - 这些也不是API。

For the others, add the package name to the Import-Package entry 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)。

#4


1  

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工具 - >首先将项目转换为插件项目来转换它。如果以正常方式(“配置构建路径”)向插件项目添加依赖项,则类加载在运行时将无法正常工作(尽管它将进行编译)。

#1


15  

I think I found an easier way to do this:

我想我找到了一种更简单的方法:

  • right-click on your project in the Package Explorer;
  • 在Package Explorer中右键单击您的项目;

  • choose "Build Path...";
  • 选择“Build Path ...”;

  • choose "Configure Build Path";
  • 选择“配置构建路径”;

  • choose the Libraries tab;
  • 选择Libraries选项卡;

  • click the "Add Variable..." button;
  • 单击“添加变量...”按钮;

  • in the list box, choose the "ECLIPSE_HOME" entry, and then click the "Extend" button;
  • 在列表框中,选择“ECLIPSE_HOME”条目,然后单击“扩展”按钮;

  • 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 beneath the folders;
  • 在列表框中,打开“plugins”文件夹条目,向下滚动,然后按住Shift键并单击文件夹下面的文件列表中的所有org.eclipse.jdt。* JAR文件;

  • click OK until you're all the way back out.
  • 单击确定,直到您完全退出。

That should do it.

应该这样做。

#2


3  

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。然后在Libraries选项卡中,使用“Add External JARs”按钮添加Eclipse插件目录中的每个相关JAR文件。

#3


2  

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

如果您正在为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.

坚持正式记录的API。仅引用已发布的API Javadoc中为该组件记录的包。永远不要引用属于其名称中具有“内部”的其他组件的包 - 这些包绝不是API。永远不要引用没有已发布的API Javadoc的包 - 这些也不是API。

For the others, add the package name to the Import-Package entry 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)。

#4


1  

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工具 - >首先将项目转换为插件项目来转换它。如果以正常方式(“配置构建路径”)向插件项目添加依赖项,则类加载在运行时将无法正常工作(尽管它将进行编译)。