简体   繁体   English

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

[英]Java jar file not running in Linux terminal

I am trying to run a .jar file in terminal on Linux but gives an error: 我试图在Linux上的终端中运行.jar文件,但出现错误:

[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? 是什么导致此错误,我该如何解决?

Try doing the following - 尝试执行以下操作-

First Step: 第一步:

sudo update-java-alternatives -l

This to ensure you have JDK6 installed correctly. 这可以确保您正确安装了JDK6。 It should display something like this - 它应该显示如下内容-

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 later as the output then you will have to do the following steps. 如果将javac 1.6.0_10 or later作为输出,则必须执行以下步骤。 If you dont get the above output, you will have to un-install and re-install java. 如果未获得上述输出,则必须卸载并重新安装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 ln -s / usr / local / java /usr/local/jdk1.6.0_10

  2. Once that is done, add the following to your .bashrc file 完成后,将以下内容添加到您的.bashrc文件中

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

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? 请注意,javac和java来自不同的目录吗? Those two need to be from the same place, the same version. 这两个必须来自同一位置,相同版本。

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的位置。

You must have compiled with a newer version of the JDK/JRE than the one you're running the jar with. 您必须使用比运行jar的版本更高的JDK / JRE版本进行编译。 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 在NetBeans中检查用于编译项目的JDK版本,然后在控制台中使用以下命令检查JDK / JRE版本:

java -version

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. 如我所见,您实际上在运行gcc java。 Try to start this jar by original sun/oracle jre 尝试通过原始的sun / oracle jre启动此jar

Run the following in your own machine and the actual machine your are trying to deploy: 在您自己的计算机和您尝试部署的实际计算机上运行以下命令:

java -version Java版本

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文件夹,然后运行命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM