Pregunta

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 ?

  1. Use an alternative "compiler" and just include the jars and use them ?
  2. Include tools.jar (that is found in the JDK folder) in my plugin, dynamically load it, and compile from it ?
  3. Any other solution that I cant think of

What would you recommend ?

¿Fue útil?
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top