Problem with JAVAC version for dynamic compilation

-1

I'm trying to compile a HelloWorld class for testing, and it's giving an error that I do not know how to solve. Does anyone know a solution? It looks like it's some version-related issues.

I'm trying to compile like this:

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 the result is this:

/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 ---------

How to solve it? What version problem is this? Added: I executed java -version and javac -version and both returned 1.8.0_73

    
asked by anonymous 21.03.2016 / 16:16

2 answers

2

I finally managed to solve my problem. It seems that by default the eclipse only imports jars from jre. So I had to add as an external jar the 'tools.jar' file that is in my jdk folder. C:\ProgramFiles\Java\jdk1.8.0_73\lib\tools.jar

After doing this I was able to compile what I wanted.

    
28.03.2016 / 16:14
0

This example might help you: link

Try adding "source", "1.5", "-target", "1.5" as compile options:

String[] optionsAndSources = {
            "-source", "1.5",
            "-target", "1.5", 
            arquivo2 
        };

int status =  Main.compile(optionsAndSources, out);
    
23.03.2016 / 19:41