Java jar 文件未在 Linux 终端中运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9014992/
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-08-06 04:13:46  来源:igfitidea点击:

Java jar file not running in Linux terminal

javalinuxjarnetbeans-7

提问by Syed Raza

I am trying to run a .jarfile in terminal on Linux but gives an error:

我试图.jar在 Linux 上的终端中运行一个文件,但出现错误:

[root@localhost dist]# java -jar helloworld.jar
Exception in thread "main" java.lang.ClassFormatError: 
   helloworld.Helloworld (unrecognized class file version)

   at java.lang.VMClassLoader.defineClass(libgcj.so.7rh)
   at java.lang.ClassLoader.defineClass(libgcj.so.7rh)
   at java.security.SecureClassLoader.defineClass(libgcj.so.7rh)
   at java.net.URLClassLoader.findClass(libgcj.so.7rh)
   at gnu.gcj.runtime.SystemClassLoader.findClass(libgcj.so.7rh)
   at java.lang.ClassLoader.loadClass(libgcj.so.7rh)
   at java.lang.ClassLoader.loadClass(libgcj.so.7rh)
   at gnu.java.lang.MainThread.run(libgcj.so.7rh).

I have checked my version and it is:

我检查了我的版本,它是:

[root@localhost /]# java --version
java version "1.4.2"
gij (GNU libgcj) version 4.1.2 20080704 (Red Hat 4.1.2-51)

What is causing this error and how do I fix it?

是什么导致了这个错误,我该如何解决?

回答by JB Nizet

You must have compiled with a newer version of the JDK/JRE than the one you're running the jar with. Check the version of the JDK you're using to compile the project in Netbeans, and then check the JDK/JRE version in the console using

您必须使用比运行 jar 的版本更高的 JDK/JRE 版本进行编译。检查您在 Netbeans 中用于编译项目的 JDK 版本,然后使用以下命令在控制台中检查 JDK/JRE 版本

java -version

回答by Odobenus Rosmarus

The error is : "unrecognized class file version" . It means you trying to start jar that was build by incompatible java version.

错误是:“无法识别的类文件版本”。这意味着您尝试启动由不兼容的 Java 版本构建的 jar。

As I can see, you actually run gcc java. Try to start this jar by original sun/oracle jre

正如我所看到的,您实际上运行的是 gcc java。尝试通过原始的 sun/oracle jre 启动这个 jar

回答by Salil.W

Run the following in your own machine and the actual machine your are trying to deploy:

在您自己的机器和您尝试部署的实际机器上运行以下命令:

java -version

版本

If it is different, then you should get the issue.

如果它是不同的,那么你应该得到这个问题。

Note: If you get cannot find command error while running above command, the path variable may not be set correctly. In this case, please go to the dir where jdk/sdk is installed, to bin folder and then run the command.

注意:如果您在运行上述命令时遇到找不到命令错误,则可能是路径变量设置不正确。在这种情况下,请转到安装 jdk/sdk 的目录,到 bin 文件夹,然后运行命令。

回答by jaypal singh

Try doing the following -

尝试执行以下操作 -

First Step:

第一步:

sudo update-java-alternatives -l

This to ensure you have JDK6 installed correctly. It should display something like this -

这是为了确保您正确安装了 JDK6。它应该显示这样的东西 -

java-6-sun 63 /usr/lib/jvm/java-6-sun

Second Step:

第二步:

sudo update-java-alternatives -s java-6-sun

This way both javac and java v6 will be used by default.

这样,默认情况下将同时使用 javac 和 java v6。

Update:

更新:

Type the following on your terminal -

在您的终端上输入以下内容 -

javac -version

If you get javac 1.6.0_10 or lateras the output then you will have to do the following steps. If you dont get the above output, you will have to un-install and re-install java.

如果您获得javac 1.6.0_10 or later作为输出,那么您将必须执行以下步骤。如果没有得到上述输出,则必须卸载并重新安装 java。

Steps to do if version shown is 1.6.0_10 or later

如果显示的版本是 1.6.0_10 或更高版本,需要执行的步骤

  1. Create symlink-

    ln -s /usr/local/java /usr/local/jdk1.6.0_10

  2. Once that is done, add the following to your .bashrcfile

    export JAVA_HOME=/usr/local/jdk1.6.0_10 export PATH=$JAVA_HOME/bin:$PATH export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib

  1. 创建符号链接-

    ln -s /usr/local/java /usr/local/jdk1.6.0_10

  2. 完成后,将以下内容添加到您的.bashrc文件中

    export JAVA_HOME=/usr/local/jdk1.6.0_10 export PATH=$JAVA_HOME/bin:$PATH export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib

回答by Eric Leschinski

You get this error if your versions of javac and java are not the same.

如果您的 javac 和 java 版本不同,则会出现此错误。

Exception in thread "main" java.lang.ClassFormatError: Runner
(unrecognized class file version)

Diagnose the problem this way:

以这种方式诊断问题:

$ which java
/usr/bin/java

$ which javac
/home2/ericlesc/bin/jdk1.8.0_40/bin/javac

Notice how the javac and the java are not from the same directories? Those two need to be from the same place, the same version.

请注意 javac 和 java 不是来自同一目录?这两个需要来自同一个地方,同一个版本。

To fix it, I added these to my .bash_profile

为了修复它,我将这些添加到我的 .bash_profile

export JAVA_HOME=/home2/ericlesc/bin/jdk1.8.0_40
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib

Then the error goes away.

然后错误消失。

You will need yours to point to where you installed java.

您将需要您的指向您安装 java 的位置。