I am trying to compile a HelloWorld class just as a test and it is showing an error that I don't know how to solve. It seems like it is something about incompatible version, but when I do java -version
and javac -version
both display 1.8.0_73.
Here is what I am trying to compile:
String arquivo2 = "/C:/classes/HelloWorld.java";
PrintWriter saida = new PrintWriter(new FileWriter("logCompilacao.txt"));
int resultadoCompilacao = com.sun.tools.javac.Main.compile(new String[]{arquivo2},saida);
But this is the output:
/C:/classes/HelloWorld.java:2: cannot access java.lang.Object
bad class file: C:\Program Files\Java\jre1.8.0_73\lib\rt.jar(java/lang/Object.class)
class file has wrong version 52.0, should be 49.0
Please remove or make sure it appears in the correct subdirectory of the classpath.
public class HelloWorld {
^
1 error
"class file has wrong version 52.0, should be 49.0". Does someone know how to solve this problem? I tried a lot but I didn't find out how to solve it.
I finally found how to solve it. It seems like by default eclipse only imports jre jars so I had to add as an external jar the file 'tools.jar' found in my jdk folder. C:\Program Files\Java\jdk1.8.0_73\lib\tools.jar
.
After that I could compile my classes