craftsman
craftsman

Reputation: 15653

Running a jar from shell script

I have a jar file named umar.jar in /root/umar/bin directory. I have a shell script file run.sh in same directory. Following is the content of run.sh

#!/bin/bash
"$JAVA_HOME"/bin/java -jar /root/umar/bin/umar.jar

Now when I run the shell script, I get the following error

Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file

After some googling, I found (as a folk from stackoverflow mentioned) that such errors occur when the Jar was compiled with a later version of the JDK than your JRE.

Now strange thing is, if I run this command directly on shell

java -jar umar.jar

it works perfectly fine. If the jar was compiled with a later version of JDK than my JRE, it shouldn't have had run at all even from the shell.

What do you suggest?

Upvotes: 2

Views: 58873

Answers (3)

atr
atr

Reputation: 714

Export the java and jre home which you need to use in your sh file.

Run the jar using the exported java home

e.g

export JAVA_HOME=/opt/java6
export JRE_HOME=/opt/java6/jre
/opt/java6/bin/java -Xms2048m -Xmx2048m -jar abc.jar $1

Upvotes: 0

denis.zhdanov
denis.zhdanov

Reputation: 3744

As already mentioned - you're trying to use byte code compiled by the later compiler with old jvm.

Please note that if your PATH contains multiple java executables of different versions you can switch between them easily using '-version' key.

Suppose you have java5 and java6 at your PATH and java5 is located before java6 there. You can see that java5 is used by default then (if you execute 'java -version' it prints corresponding information). However, you can start java6 easily using command like 'java -version:1.6 ...' (e.g. if you execute 'java -version:1.6 -version' you see that java6 is used).

Upvotes: 1

jqa
jqa

Reputation: 1370

compare "java -version " and ""$JAVA_HOME"/bin/java -version"

You probably have multiple JVMs installed

Upvotes: 4

Related Questions