How can I import eclipse JDT classes in a project
Asked Answered
I

4

4

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.

Interrupter answered 18/9, 2008 at 15:44 Comment(2)
Did any of the solutions work for you? Curious what you ended up doing.Fetich
Are you going to accept an answer yet? Or did none of them work? If so, then please comment on those answers or make an edit to your question to explain why the answers are not working.Kancler
F
15

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.

Fetich answered 18/9, 2008 at 18:29 Comment(4)
That might not survive an Eclipse migration, as you're going to be adding version-specific JDT JAR files to your classpath.Geochronology
JDT can be used in stand-alone app, but it is a good practice to use it in a Plug-in. Because you also frequently need Java Model which is only supported in a plug-in.Deportment
I've found the jar files in my local P2 repo: e.g. %USERPROFILE%\.p2\pool\pluginsKimbrakimbrell
Just add into pom <dependency> <groupId>org.eclipse.jdt.core.compiler</groupId> <artifactId>ecj</artifactId> <version>4.6.1</version> </dependency>Side
F
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.

Fetich answered 18/9, 2008 at 15:53 Comment(2)
Is there a way to add the whole of Eclipse's path to the classpath and/or buildpath and not add each individual jar?Interrupter
See my next answer -- I think I found an easier way.Fetich
I
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

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.

Importunacy answered 18/1, 2009 at 0:32 Comment(0)
H
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).

Hidie answered 18/9, 2008 at 19:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.