Reputation: 1189
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 ?
What would you recommend ?
Upvotes: 0
Views: 1028
Reputation: 170919
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.
Upvotes: 1