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
}