Question

I'm getting this error:

enter image description here

due to my Java version. I would like to make a .jar file compatible with older versions of Java as well as newer versions. My PC isn't a problem but my audience uses older versions of Java. How could I make my program compatible with older versions as well as newer versions and how far should i go with it?

Was it helpful?

Solution

You could go back and compile your sources and generate a new jar file with an older javac thats compatible with the particular version of java.

OTHER TIPS

Either compile you jar using an old compiler or set the java target to the version your clients support.

To set the version, follow this thread: Setting the target version of Java in ant javac

UnsupportedClassVersionError isn't about the JAR (a JAR is simply a zip archive), but instead the classes inside the JAR.

.class files are just compiled Java sources and are in a format called ByteCode that the JVM (Java Virtual Machine) uses to perform operations on the underlying platform. Bytecode's format changes every once in a while (Java has been around since 1991) and thus the version numbers that are hard-coded into compiled classes formats usually change between JRE releases.

tl;dr: What you'll need to do is acquire the source and re-compile against the JRE you want. There is no way to just tell the class files to change or be compatible with older versions. Archived JRE versions are available but you'll have to register for an account to download versions other than the latest.

EDIT

I wasn't paying attention to the version number. Your .class files are compiled with J2SE 1.8 and you're running the JRE7. Install the JRE 1.8.

The only way to be sure your code is actually compatible with an older major java version is to compile under a JDK of that version. While its possible to compile for an older JVM version, you can never be sure you don't use methods/classes that do not exist in an older JRE if you use a newer JDK to compile against.

To ensure your code actually runs fine with a newer JRE, you will need to test it with the newer JRE. No way around that, while newer JRE's retain great backward compatibility there are some slight differences that can trip your code up.

You can use NetBeans IDE for this purpose.

Run>> Set Project Configuration >> Customize >> In the left panel, choose Sources. Finally, select your minimum JDK version in Source/Binary Format.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top