Search code examples
javaeclipseeclipse-jdt

Diagnose "incorrect classpath" indicated by java compiler


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:

  • propertiesFile: exists and it contains the merge of the workspace + project's settings without repetition (in that order).
  • classPath: contains multiple paths separated by ";" (this one has problems, see below)
  • fileToCompile contains the absolute path of the file I want to compile. Do note that this file is not in the sources directory.
  • outputPath: The directory where the "bin" of the project is. It gets it from the IProject object itself.

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:

  1. "D:/Users/user/runtime-EclipseApplication/Tests/bin"
  2. "D\:\Users\user\runtime-EclipseApplication\Tests\bin"
  3. "D\\:/Users/user/runtime-EclipseApplication/Tests/bin"
  4. "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:

  1. That classpath directory exists
  2. That Test.class is in that directory
  3. I'm using the default package at the time that code executes

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.


Solution

  • 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.