Setting required Java version for .jar file to run with? - java

I have a program that cannot run as long as Java 8 is installed on my system. Can I specify which version of java that should run that specific .jar file, so I don't have to uninstall it each time I'm going to run the program?

you could check the Java Version at the start of your main class and exit it if a version lower then 1.8 is installed.
take a look here Getting Java version at runtime

Related

Launch4j-FakeMain compiled by more recent version Java Runtime(class file version 55 0)this Java Runtime only recognizes class file version up to 52 0

This is my first application that i try to execute exe.
I'm using launch4j to do this. After executing and trying to run, I get this error:
Blockquote
Launch4j - Exception in thread "main" java.lang.UnsupportedClassVersionError: FakeMain has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
I searched something about versions of java, so I checked my versions of java and they are the same.
That's my versions of java:
My versions of java in cmd
Maybe I did something wrong there?
Screen of Launch4j
I don't know what I can do. I spend all this day about fix this and nothing.
Error after try to run exe file
You have compiled the code with Java 11 as target. But your PC want's to execute the Java Code with Version 8, which is not possible.
Make sure you have installed the correct Versions and also make sure that the executable uses Java 11.
Here you can find the Java Versions mapped to the Runtime Versions: Java Version Numbers
The Article gives you also some information about how to compile and execute for the correct versions.

JAR file I created runs only on computers with JDK installed

I recently created an application using the NetBeans IDE. I deployed it and created a jar file that runs perfectly on my computer and any computer that has the Java JDK installed, but when I try to run it on another computer that only has the JRE installed it says:
"Error: A JNI error has occurred, please check your installation and try again"
and then
"A Java Exception has occurred."
If you have JDK that you used to create jar with version1 and jre with version2 then you will encounter this kind of error.
For example:
If i used Java JDK 12 installed on my computer#1 to create jar.
At the same time, I had Java 8 (JRE) Installed on my friends computer.
So on my friend's computer it is not working because it has jre8..
I typed the follow commands to troubleshoot :
On computer#1 :
javac -version
// this gave me Java 12
On computer#2
java -version
// this gave me Java 8
In other words, my program is getting compiled with Java 12 (computer1)and I am trying to run with Java 8 on computer2.
To solve the problem, try running jar on same version of java used for development. Means match jdk and jre version.

Java installation in mac

I am new to mac. Running the javac / java -version command yields negative result. However, I do see java under system preferences. I have not installed JDK/JRE yet. On clicking the java icon, under update, it shows that the system has the recommended version of java. Java 8 update 144. What is the java icon doing under system preferences when I have not installed JDK/JRE.
It is JRE, which comes along with OS by default. I'm not sure, how did you executed java -version command. In my case, I get proper information about installed JRE.
You probably need to check your User and System variable under Java
Also, it is obvious to get error when executing javac without installing JDK.

How can I change java version on local linux server

I built my NetBeans web project with Java 1.5 successfully, however; my linux server supports / uses Java 1.4 and Java 1.5 (as well as JBoss 4.0.2).
When I check the version of my project ( java -version ) it says that the current version is Java 1.4.2. However, I don't want to change the "JAVA_HOME" setting on the server because other projects need to use this version.
I want my project to use Java 1.5 from the server...
An idea as to how I should go about doing this? Is there a configuration that I can change?
I have this error:
java.lang.UnsoupportedClassVersionError: bad Version in .class file
You need Java 1.5 version installed in your Linux server.
Next, run the following set of commands in your terminal/command prompt:
JAVA_HOME="{fix-me}"
export JAVA_HOME
export PATH = $JAVA_HOME/bin:$PATH
In the place holder {fix-me} specify the path of Java 5 that is installed in the Linux machine.
This would only temporarily set the Java version to 5 until the terminal/command prompt session is alive. So you need not worry about disturbing other projects.

Java .jar not running in unix

I've created a executable jar file in an Windows environment. I'm able to run this jar without any problems in Windows.
When I try and run the same jar in a unix environment, I get the following exceptions:
Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:242)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:56)
What am I doing wrong?
You are trying to run it under an earlier version of Java.
You can compile targeting the correct version, or run under a newer version.
Looks like you're building on Java 7 and then trying to run on Java 5, or something like that. It's not a matter of Windows vs Unix - it's a matter of the version of Java within the operating system.
Ideally, upgrade your Unix system to a recent release of Java - or if you can't do that, change how you're building to use the -target flag in javac to generate appropriate bytecode. You should target the right version of the standard libraries for the environment you need to run in, too.
You are trying to run a program compiled on a newer version of Java on an older version of Java.
Make sure the program is compiled for the appropriate target version that you will be running on. You can set compile options to use a newer compiler to create programs that will run on older versions, but obviously you can't use newer features.
For example: Java 7 runs all previous versions of binaries. Java 5 can't run newer versions of binaries.
Make sure you have on both platforms the latest version of Oracle Java installed.
UnsupportedClassVersionError means that you compiled the class with a version of Java that is newer than the version of Java that you are trying to run the program on.
For example, you have compiled this with JDK 6, and you are trying to run the program on a Java 5 or older JVM.
That doesn't work. Newer versions of Java are able to run programs compiled with older versions, but not the other way around.
It's likely that you have an older version of Java on your *nix install as opposed to your Windows install. The jar was built using a newer version, meaning it can't run on the old version because it may have features that aren't supported yet. Try updating your *nix Java and try running it again.
It looks like the .class file was compiled with a newer version of java then your linux installation. please update the results of:
java --version
on both linux and windows, and check if you have java, and not openjdk, on linux (openjdk might bug).
if the version on linux is lower then on windows, that is the problem, update it.
Most likely you are trying to execute a jar file compiled with a compiler with a newer version than the JVM installed on the system where you are trying to execute it. Check JVM version on the Linux system and make sure to have a version equal or newer than the version of the compiler ...
(Posted on behalf of the question author).
Thanks for the help, it seems you all are correct. I developed the jar on java 1.6 and our unix server is on 1.5.

Categories