Java jar file not running in Linux terminal

半腔热情 提交于 2019-12-11 01:38:58

问题


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

[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?


回答1:


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 -

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.

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. If you dont get the above output, you will have to un-install and re-install java.

Steps to do if version shown is 1.6.0_10 or later

  1. Create symlink-

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

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

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




回答2:


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

java -version



回答3:


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

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




回答4:


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.




回答5:


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

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.

To fix it, I added these to my .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.



来源:https://stackoverflow.com/questions/9014992/java-jar-file-not-running-in-linux-terminal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!