命令提示中的 Java 编译错误:类文件版本错误 52.0,应为 50.0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29906659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:06:42  来源:igfitidea点击:

Java Compiling Error in Command prompt: class file has wrong version 52.0, should be 50.0

javacompilationversion

提问by Teva Velu

I create a java library program and used it in another java program as .jar file. My IDE is NetBeans. I tried the same concept via command line and I got the following error:

我创建了一个 java 库程序并在另一个 java 程序中使用它作为 .jar 文件。我的 IDE 是 NetBeans。我通过命令行尝试了相同的概念,但出现以下错误:

class file has wrong version 52.0, should be 50.0 Please remove or make sure it appears in the correct sub directory of the class path. import Demo1_Lib.Test1; ^

class 文件有错误的 52.0 版本,应该是 50.0 请删除或确保它出现在类路径的正确子目录中。导入 Demo1_Lib.Test1; ^

This are my steps.

这是我的步骤。

Step 1:Created the following class library in NetBeans IDE.

步骤 1:在 NetBeans IDE 中创建以下类库。

  package Demo1_Lib;

 /**
  *
  * @author tveluppillai
 */
public class Test1 
{
  public void print() 
  {
    System.out.println("hello");
  }    
 }

Step 2:Create a java project on netbeans and add the jar file. (Test1.jar) and consume the class library function.

第二步:在netbeans上创建一个java项目,并添加jar文件。(Test1.jar) 并消费类库函数。

 package test2;

 import Demo1_Lib.Test1;

 /**
 *
    * @author tveluppillai
 */

  public class Test2 
  {

   /**
    * @param args the command line arguments
    */
    public static void main(String args[]) 
   {
     Test1 obj = new Test1();
     obj.print();       
    }

 } 

This compiles fine and when I ran, it gives me the right output in NetBeans

这编译得很好,当我运行时,它在 NetBeans 中为我提供了正确的输出

However, when I do the same thing using command prompt I got error.

但是,当我使用命令提示符做同样的事情时,我得到了错误。

I used the following command to compile and run it.

我使用以下命令编译并运行它。

javac -cp C:\Demo_Lib\Test\Test1.jar Test2.java

I got the following error:

我收到以下错误:

class file has wrong version 52.0, should be 50.0 Please remove or make sure it appears in the correct sub directory of the class path. import Demo1_Lib.Test1; ^

class 文件有错误的 52.0 版本,应该是 50.0 请删除或确保它出现在类路径的正确子目录中。导入 Demo1_Lib.Test1; ^

What am I missing?

我错过了什么?

回答by rghome

You are trying to run/reference a class compiled with JDK 8 using a runtime/compiler JRE/JDK 6.

您正在尝试使用运行时/编译器 JRE/JDK 6 运行/引用使用 JDK 8 编译的类。

The Java being used by the command line is probably a different version than the one used by NetBeans.

命令行使用的 Java 版本可能与 NetBeans 使用的版本不同。

See Java class filefor a list of what the numbers mean.

有关数字含义的列表,请参阅Java 类文件

Download JDK8, or if you already have it, add it to your path and set JAVA_HOME.

下载 JDK8,或者如果您已经有了它,请将其添加到您的路径中并设置 JAVA_HOME。

In Unix:

在 Unix 中:

export JAVA_HOME=directory
export PATH=$PATH:$JAVA_HOME/bin

回答by Teva Velu

After I change the JDK8 to my JAVA_HOME, I was able to compile the following way and run the code...

在我将 JDK8 更改为我的 JAVA_HOME 后,我能够通过以下方式编译并运行代码...

Compile:

编译:

     javac -cp C:\Demo_Lib\Test\Test1.jar Test2.java

Run:

跑:

     javac -cp C:\Demo_Lib\Test\Test1.jar;**.** Test2.java