Issue with JAVAC version for dynamic build

Asked

Viewed 177 times

-1

I’m trying to compile a Helloworld class for testing, and it’s giving an error that I don’t know how to solve. Does anyone know a solution? From the looks of it, it’s some version-related problem.

I’m trying to compile it 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 fix it? What version problem is this? Added: I executed java -version and javac -version and both returned 1.8.0_73

  • 2

    Trying to compile using different Java version. Just hang on, major version 49? This is Java 5, who even uses this o_0

  • But my java is in the latest version. This would be a problem in the javac jar?

  • 2

    No no, your Java is 52(Java 8), the source that was built with Java 5. You can see that the first Java folder that appears in your %PATH% and JAVA_HOME(if you have) is Java 8 as well.

  • @DH. what do you mean by "first java folder of my "%PATH%?" If you are referring to the Path environment variable, it looks like this: C: Programdata Oracle Java javapath;;%JAVA_HOME% bin and my JAVA_HOME looks like this: C: Program Files Java jdk1.8.0_73

  • 1

    @Araújofilho just a hint, constantly editing without adding anything in the question does not help much. Wait until someone can help you, editing costante makes it look like you are editing purposely so that it gains prominence.

  • Got it. Thanks @Diegof

  • Rotate the commands java -version and javac -version and add to the question, the problem may be different verses between jdk and jre.

  • There, I made this new edition to add the content to the question @Diegof

Show 3 more comments

2 answers

2


Finally I managed to solve my problem. It seems that by default the eclipse matters only jre jars. 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 that I was able to compile what I wanted.

0

It is possible that this example will help you: https://stackoverflow.com/a/12246348/4219136

Try to add "-source", "1.5", "-target", "1.5" as compilation options:

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

int status =  Main.compile(optionsAndSources, out);
  • Unfortunately it did not work for my case. The error remains the same

Browser other questions tagged

You are not signed in. Login or sign up in order to post.