Java .jar not running in unix - java

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.

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.

java.lang.InternalError: platform encoding not initialized when running EXE4J .exe w/ Java14 on PATH

So this error is a weird one...
I'm using EXE4J 6 to build a .exe file for my JavaFX Application. This has worked with no issues through Java version 13.0.1. I recently upgraded my environment to use Java 14.0.1 and now I get the following stacktrace whenever I try to run my application through exe:
java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.InternalError: platform encoding not initialized
at java.base/java.net.Inet6AddressImpl.getLocalHostName(Native Method)
at java.base/java.net.InetAddress.getLocalHost(Unknown Source)
at org.apache.logging.log4j.core.util.NetUtils.getLocalHostname(NetUtils.java:54)
at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:612)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:691)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:708)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:263)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:243)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:174)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:669)
This error happens when trying to initialize my Log4J logging. however if I just made a direct call to InetAddress.getLocalHost() I can replicate this error out of .exe. Running my application directly out of my Eclipse (EE 06/2020) works just fine. After doing some debugging. I determined that removing Java from my PATH allowed the application to run. My .exe4j configuration file is set up so that the application should look for a separate Java 14 jre and not attempt to use my PATH for java.
<searchSequence>
<directory location="../java/jre64" />
</searchSequence>
The jre64 is built through ANT using the 14.0.1 jdk that I have and only importing the modules I need. Again, this all worked through 13.0.1 prior to my upgrade to 14.0.1
From research and testing on other computers, I think this boils down to an environment issue on my computer. However I've run out of places to look. Any thoughts or ideas would be much appreciated. Specifically why EXE4J would try to use the Java on my Path instead of the one in the search sequence.
Other Notes:
Running on Windows 10 Latest Updates
Path points to OpenJDK 14.0.1 (causes break) (java -version is correct in cmd)
I've confirmed that the working exe, after removing java from my Path, is still running on version 14.0.1 through the jre64
Application is built with and Eclipse Workspace/project JRE also points to same OpenJDK 14.0.1
Running on other computers with similar versions & environment works & doesn't cause this issue.
I was finally able to determine what the issue was.
I was using Exe4J 6.0 which was not compatible with Java versions 10+. I was surprised that I wasn't getting outright errors when trying to run exe4j to compile my executable, however it seems that exe4j was sucking in an older 1.8 java version from my registry and using a 1.8 jdk that I never cleaned out of my "C:/Program Files/Java" folder. When I deleted all my old JDKs, exe4j started complaining about missing a Java VM (even though 14.0.1 was set on path).
Upgrading to Exe4J 7.0 solved the issue for me.
In my case the problem was the PATH environment variable pointing to one jre directory.
I had 2 applications in different directories:
Application A had a jre directory included in the PATH environment variable and was working fine.
Application B had another jre directory NOT included in the PATH environment variable and was throwing this error.
After removing this jre directory from the PATH environment variable everything worked ok.

File reading using OpenCSV

I use OpenCSV for CSV files reading and writing. It worked fine until I hsd to install earlier version of Java - i.e. Java 6. After installing it, my code stopped working:
Exception in thread "main" java.lang.UnsupportedClassVersionError:
com/opencsv/CSVReader : Unsupported major.minor version 51.0 at
java.lang.ClassLoader.defineClass1(Native Method) at
java.lang.ClassLoader.defineClassCond(Unknown Source) at
java.lang.ClassLoader.defineClass(Unknown Source) at
java.security.SecureClassLoader.defineClass(Unknown Source)
Check the version of the class file in your OpenCSV .jar dependency, for example:
javap -verbose -cp opencsv.jar com.opencsv.CSVReader | grep major
If this shows a version newer than 50 (= newer than Java 6) then you cannot use this jar.
You need to get a .jar file built with Java 6 or older. (If it can be built with Java 6 at all.)
I just confirmed that the latest version of the source code doesn't compile with Java 6,
as it uses Java 7 features (naturally).
But not too long ago, as of August 2014 the project was still on Java 5.
So if you download an older version of the jar it should work.
In other words, since you downgraded your Java version,
you also need to downgrade your OpenCSV dependency.
UPDATE
From SourceForge, version 2.3 contains a jar file that should be suitable for you:
$ javap -verbose -cp deploy/opencsv-2.3.jar au.com.bytecode.opencsv.CSVReader | grep major
major version: 50
Note that the package name is a bit different from the current version.
When OpenCSV was upgraded to 3.0 the decision was made to upgrade to Java 7. This was done for several reasons. The main reason being that the largest reason was that the main fixes in 3.0 was to support the lazy checks in Lambdas that are in Grails (possibly Java 8) so it was requested the code be compiled in Java 7 because it is more optimal for Grails. I allowed that because Oracle had announced it was dropping support for Java 7 in 2015 and Java 5 & 6 had long since been unsupported plus I had a small number of emails asking when I was going to port opencsv to java 7 (and only one for java 8 for some reason) so I felt that moving to java 7 was safe. The last was that I develop using a mac and no longer have access to a java 5 compiler and I really did not feel good about pushing out a Java 5 version of opencsv built using java 7.
In hindsight seeing the issues that this caused (you are the third that I know of that cannot upgrade because of this) I would have left it at Java 5 and asked the Grails users to perform a performance test and myself build opencsv in a Ubuntu VM on my mac. But since it is out I don't want to downgrade and possibly effect users as well.
If you cannot upgrade your project to java 7 you do have two options: the first is that on the sourceforge and maven repositories you can download the older versions. So 2.4 and earlier was built using java 5. The other option is that you can download the code and build it yourself. Thanks to KommradHomer the current code base is compatible with Java 6 and will build as is with a Java 6 compiler (you just need to change the pom file).
I am seeing the same issue. Please let me know which version of openCSV I can use to be compatible with java version "1.6.0_22" ? OpenCSV 2.3 does not work for me.

Unsupported major.minor 52.0 exception in Java

Given such an exception:
java.lang.UnsupportedClassVersionError: com/sun/tools/javac/Main : Unsupported major.minor version 52.0
And I know this is because that the JDK and JRE are not compatible. However, when I type in:
which java
The output is:
/usr/java/jdk1.6.0_24/bin/java
And when typing in:
which javac
The output is:
/usr/java/jdk1.6.0_24/bin/javac
It is the compatible in version. So what's the problem?
The problem is typically caused by this sequence:
Code is compiled with new JDK, and "target" set to current version of Java.
Attempt to run the code with an older JDK or JRE.
In this case, minor version 52 means that something has been compiled with a Java 8 JDK, and with Java 8 as the target platform, and that you are attempting to run it on an older JRE / JDK.
You could also have gotten a JAR file from somewhere else that was compiled for Java 8.
To solve your problem, you need to figure out:
which class file or files have the version number problem (the exception message should tell you the first one ...),
where the class file came from (from a JAR file? from the file system?), and then
how come it was compiled with Java 8 though you thought you were compiling with Java 6.
It is an indisputable fact that the class that won't load was compiled using Java 8 (or later) compiler. You need to figure out why, and then either recompile it with the older JDK, or run it with Java 8.
it says version 52.0 so you should install java 8.
Check if tools.jar used during compilation complies with the JRE set. If you are on eclipse, verify the versions as:
Goto Window->Preferences
Check Ant Runtime by selecting Runtime under Ant in the left pane. Check the Global Entries under classpath tab. You can see the version in folder path.
On the left pane check installed JREs under Java. Check the version.

Jar executed in eclipse but not running in Unix

I have a Jar which is running in fine in eclipse and cmd prompt.
When i tried executing the same in Unix , it is not executing.
Note: Java version in Unix is 1.5 ; Java version in eclipse is 1.7
java -jar text.jar
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 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
You answered the question yourself. While compiled at 1.7 you try to execute the jar in an 1.5 environment. Update your Unix JRE to 1.7 and it should work.
Alternatively you can rebuild the jar at 1.5. In eclipse, right click at your project in the project browser => properties => java compiler => compiler compliance level.
You get the UnsupportedClassVersionError when you are trying to run an application having a jar in the classpath that is built using a newer version than the one you are using at runtime.
You get this error when you try to run a class that was compiled for a Java version newer than what you have; for example, if you try to use a class that was compiled for Java 6 or newer on a Java 5 JVM.
It doesn't necessarily have to be your own class; you might be using some library that was built for Java 6 or newer.
Are you using specific libraries (JAR files)? Check if these are compatible with Java 5 or not. Or upgrade your Java version to Java 6

Categories