Running a jar from a shell script

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

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

      

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

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

      

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

Now it is strange if I run this command directly on the shell

java -jar umar.jar

      

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

What do you suggest?

+2


source to share


3 answers


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



You probably have multiple JVMs installed

+4


source


As mentioned, you are trying to use byte code compiled by a later compiler with an old jvm.

Note that if your PATH contains multiple java executables of different versions, you can easily switch between them using the -version switch.



Suppose you have java5 and java6 on your PATH and java5 is before java6. You can see that java5 is the default then (if you do "java -version" it prints the relevant information). However, you can easily start java6 with a command like "java -version: 1.6 ..." (for example, if you run "java -version: 1.6 -version", you can see that java6 is being used).

+1


source


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

Start jar using java exported home

eg

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

      

0


source







All Articles