0

I am creating an Eclipse plug-in that is compiling JAVA files upon click of the plug-in button.

Thanks to this StackOverflow Question, MadProgrammer gave the perfect solution for the requirement. I now have a project that can compile and run classes from an external folder.

Now, I am trying to port this into a plug-in. I have created the commands and tried to plug the same code into the plug-in code but nothing happens. I found out that task.call() is not working.

The code by MadProgrammer that works in a JAVA project class:

     DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
     JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
     StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
     List<String> optionList = new ArrayList<String>();
     optionList.add("-classpath");
     optionList.add(System.getProperty("java.class.path") + ";dist/InlineCompiler.jar");
     Iterable<? extends JavaFileObject> compilationUnit = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(listOfFiles));
     JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null, null, compilationUnit);
     if (task.call()) { 
           //insert logic here 
     }

The only difference that I found is that if I run the code in a JAVA class, System.getProperty("java.class.path") returns the build path of the JAVA project. However, when I run the code as a plug-in, the same code returns: C:\Users\xxxxxx\Desktop\eclipse\plugins\org.eclipse.equinox.launcher_1.3.100.v20150511-1540.jar

I am quite unsure if this is really the reason why compiler.getTask() isn't working. Aside from that, everything else is the same: this includes the classes to be compiled, the jdk version, etc.

Any input or advice on what should I do is greatly appreciated!

Thank you!

Update: I found out that the JAVA project works even without:

 List<String> optionList = new ArrayList<String>();
 optionList.add("-classpath");
 optionList.add(System.getProperty("java.class.path") + ";dist/InlineCompiler.jar");

and setting null at the option list for compiler.getTask(), so I'm looking into the files that are being compiled, but so far, they're the same with the exception for the name of the package in the implements.

2nd Update: Diagnostics showed the following errors: package xxxxplugin_handlers.CheckHandler does not exist and method does not override or implement a method from a supertype.

Here's a snippet of the file I am trying to compile:

public class checkClass implements xxxxplugin.handlers.CheckHandler.DoStuff{

@Override
public String doStuff(String code)
{
    //class function here
}
Community
  • 1
  • 1
Kawamoto Takeshi
  • 596
  • 1
  • 3
  • 24
  • How is it 'not working'? Do you get messages? If so what are they? – greg-449 Jun 22 '16 at 07:14
  • Hi @greg-449, basically, task.call returns false and no .class files are generated. – Kawamoto Takeshi Jun 22 '16 at 07:16
  • @greg-449, it seems I have forgotten about the part where it shows the diagnostics when the call fails. I added it and it shows that: my errors are `public class checkClass implements xxxxplugin.handlers.CheckHandler.DoStuff` and `@Override` – Kawamoto Takeshi Jun 22 '16 at 07:27
  • @greg-449: Further looking into it, it gives this error message: `package xxxxplugin.handlers.CheckHandler does not exist.` and `method does not override or implement a method from a supertype` which is weird. I have the class created along with the 'supertype' doStuff. – Kawamoto Takeshi Jun 22 '16 at 07:33
  • Eclipse plug-ins have their own specialized classpath so you may need to do more work to set up the compilation classpath. I am not an expert on JavaCompiler so I don't know how you would do that. – greg-449 Jun 22 '16 at 07:48
  • Hi @greg-449. I'm confused on why it says package does not exist when it clearly exists and handlers under that package are working fine. Are there any known issues similar to this? – Kawamoto Takeshi Jun 22 '16 at 08:26

0 Answers0