Can I import org.eclipse.jdt.core into Netbeans?

2020-02-16 04:12发布

问题:

I am using a this project on the project that I am developing in Netbeans. Basically, I copied the 3 source code files to my project and did some tweaks to the Main.java file in order for me to be able to use it in my project.

The compiler then said that package org.eclipse.jdt.core.compiler does not exist for the following imports:

import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;

I tried to add the module dependency by going to Project Properties > Libraries > Module Dependencies > Add and typing org.eclipse.jdt.core.compiler but no library was found. Usually, the available libraries are shown as one types their name. In this case, none was shown.

So, I downloaded org.eclipse.jdt.core jar from here and added it to the project by going to Project Properties > Libraries > Wrapped JARs > Add JAR. The compilation errors disappeared and I was able to run the project.

However, when the line ASTParser parser = ASTParser.newParser(AST.JLS3); is reached, the following exception is thrown.

Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/core/resources/IResource

I am supposing that this exception and the fact that I couldn't find the library in the module dependecies in Netbeans are related.

Does this mean that I cannot use org.eclipse.jdt.core in Netbeans or is there another problem? Can someone help me?

回答1:

To use the Eclipse ASTParser in a plain (non-OSGi, not Eclipse-based) Java application you need following JARs:

  • org.eclipse.core.contenttype
  • org.eclipse.core.jobs
  • org.eclipse.core.resources
  • org.eclipse.core.runtime
  • org.eclipse.equinox.common
  • org.eclipse.equinox.preferences
  • org.eclipse.jdt.core
  • org.eclipse.osgi
  • org.eclipse.text

All these JARs can be found in an Eclipse SDK ZIP file that you can download from the Eclipse Project Downloads website, e. g. Eclipse 4.7.3a SDK. Since these JARs are not platform-dependent, it doesn't matter which operating system you choose. For example, you can choose eclipse-SDK-4.7.3a-win32-x86_64.zip.

In the ZIP file, you will find the JARs in the subdirectory plugins as <id>_<version>.jar (the corresponding source code as <id>.source_<version>.jar).