Javac for dynamic compilation does not work

I'm trying to compile a HelloWorld class as a test, which shows an error I don't know how to solve It seems that it is about incompatible versions, but when I do java - version and javac - version, it shows 1.8 0_ seventy-three

This is what I want 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

The class file has the wrong version 52.0, which should be 49.0 Does anyone know how to solve this problem? I tried a lot, but I didn't find out how to solve it

Solution

You have added some external from the other side so that you can run the java compiler

This is not a good way Most likely, you only have JRE (runtime environment) installed

Instead, you should download and install the JDK (Development Kit) The latter has javac; You can rest assured that its "javac" and "Java" binaries work well together

So: make sure you start in the right environment; It's not something you brought here or there

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>