JavaCompiler cannot find symbol

1k views Asked by At

I have a similar issue like that JavaCompiler not compiling files properly. I get also the error error: cannot find symbol but I use my own interface that the class should implements.
My class looks like:

   String className = "refac.MyClass"; 
   String javaCode = "package refac;\n" +
               "import refac.IBewertungsAlgorithmus;\n" +
                  "public class MyClass implements IBewertungsAlgorithmus{\n" +
                       "    public Boolean Bewertung (int checkliste){\n" +
                        "        System.out.println(\"Hello World\");\n" +
                        "        return false;   }\n" +
                    "}\n";


I get this compilation error:

[/refac/MyClass.java:2: error: cannot find symbol
import refac.IBewertungsAlgorithmus;
            ^
  symbol:   class IBewertungsAlgorithmus
  location: package refac, /refac/MyClass.java:3: error: cannot find symbol
public class MyClass implements IBewertungsAlgorithmus{
                                ^
  symbol: class IBewertungsAlgorithmus]


The class and the interface are in the same package - seemingly.

System.getProperty("java.class.path") returns
C:\Users\Plath\Axon\Neu\AxonIvyDesigner\AxonIvyDesigner6.7.1.55496_Windows_x64\plugins/org.eclipse.equinox.launcher_1.3.0.v20140415-2008.jar;

This isn't the project path. How do I get the class path of a web projekt?

1

There are 1 answers

0
Tokra On

I found a way to create the correct class path to be the same as the runtime's.

     ClassLoader dummyc=getClass().getClassLoader();
     URLClassLoader urlClassLoader=(URLClassLoader)dummyc;
     URL[] urls=urlClassLoader.getURLs();
     String classpath = "";
     for (URL i : urls) {
         classpath += ";" + i.getPath().substring(1);
     }
     optionList.addAll(Arrays.asList("-classpath",System.getProperty("java.class.path") 
+ classpath )) ; 

Now the class is compiled correctly.