Out of desperation of lack of ideas I'm currently working on a way to compile a single file from inside an eclipse plugin.
So far, I've successfully made code that compiles a single .java file that has no external .class
(without being inside .jar
) dependencies.
For the compilation process, I know where all the dependency .class
files are and I give that information to the compiler using -classpath
option.
Currently, I'm calling the compiler like this:
String[] params = new String[]{
"-properties", propertiesFile,
"-g", "-preserveAllLocals",
"-classpath", classPath,
fileToCompile,
"-d", outputPath,
"-proc:none",
"-proceedOnError",
};
boolean result = BatchCompiler.compile(
params, new PrintWriter(outWriter), new PrintWriter(errWriter), null);
The variables:
You may find the meaning of the other options here.
classPath
is giving me an error. Two classes exist in this test project:
This is its content (after reducing the size by removing most .jar includes from native java:
"C:/Program Files (x86)/java/jre1.8.0_66/lib/resources.jar";"C:/Program Files (x86)/Java/jre1.8.0_66/lib/rt.jar";D:/Users/user/runtime-EclipseApplication/Tests/bin"
I've tried using these as the last "include" in the classpath:
"D:/Users/user/runtime-EclipseApplication/Tests/bin"
"D\:\Users\user\runtime-EclipseApplication\Tests\bin"
"D\\:/Users/user/runtime-EclipseApplication/Tests/bin"
"D\\:\\Users\\user\\runtime-EclipseApplication\\Tests\\bin"
Here's the output it gives in stderr:
incorrect classpath: "D:/Users/user/runtime-EclipseApplication/Tests/bin"
----------
1. ERROR in D:\Users\user\runtime-EclipseApplication\.metadata\.plugins\myplugin\tmp\sources\Test2.java (at line 3)
public class Test2 extends Test{
^^^^^^^^
Test cannot be resolved to a type
I can assert in my own code that, just before the compiler is called:
What am I doing wrong here? Why is it classifying it at an incorrect classpath?
I'm using org.eclipse.jdt.core(v.3.10.2) dependency and I'm compiling in eclipse Luna (4.4) which is the minimum version I want my plugin to support.
After days around this, somehow...
"C:/Program Files (x86)/java/jre1.8.0_66/lib/resources.jar";"C:/Program Files (x86)/Java/jre1.8.0_66/lib/rt.jar";D:/Users/user/runtime-EclipseApplication/Tests/bin
<- No ending quote
That worked. I don't remember anymore if I did anything else but it works like that regardless if it has spaces or not.
I'm still wondering what is causing the inconsistency between the first elements using quotes but my element mustn't use any quotes.