You can use Eclipse JDT compiler, as described at http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Fguide%2Fjdt_api_compile.htm.
Compile Java classes at runtime of Eclipse Plugin
-
31-05-2022 - |
Frage
I'm in a very awkward situation here. Im developping an 'Eclipse plugin' that has to 'compile' .java files into .class files (or maybe even a jar).
I am trying to use the Java Compiler API in order to do something like this :
public class Compiler {
private JavaCompiler _javaCompiler;
private StandardJavaFileManager _sjfm = null;
private Files[] _filesIn;
public Compiler()
{
_javaCompiler = ToolProvider.getSystemJavaCompiler();
_sjfm = _javaCompiler.getStandardFileManager(null, null, null);
}
public void CompileFiles(ArrayList<String> strings, String outputFolder)
{
//_javaCompiler.run
Iterable fileObject = _sjfm.getJavaFileObjectsFromStrings(strings);
String[] options = new String[]{"-d", outputFolder};
_javaCompiler.getTask(null, null, null, Arrays.asList(options), null, fileObject).call();
try {
_sjfm.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The problem is, when I call _sjfm = _javaCompiler.getStandardFileManager(null, null, null);
I run into a NullPointerException because, this is an 'expected' behavior when not running the JDK (please refer to this bug report)
This StackOverflow post helped me in some way, but what would be the correct way to compile Java files from within an Eclipse Plugin that must be publishable ?
- Use an alternative "compiler" and just include the jars and use them ?
- Include tools.jar (that is found in the JDK folder) in my plugin, dynamically load it, and compile from it ?
- Any other solution that I cant think of
What would you recommend ?
Lösung
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow