How can I import eclipse JDT classes in a project

让人想犯罪 __ 提交于 2020-01-11 03:16:07

问题


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.


回答1:


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 beneath the folders;
  • click OK until you're all the way back out.

That should do it.




回答2:


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.




回答3:


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

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.

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

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.

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.




回答4:


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).



来源:https://stackoverflow.com/questions/93728/how-can-i-import-eclipse-jdt-classes-in-a-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!