I have created a Java Swing application in eclipse. And I have packaged the application into Runnable JAR with the help of Eclipse Export functionality. But I am unable to run the JAR in other machines. In my system it is working fine.
How to make it independent?
The default output folder(i.e for class files) : Project_Name/bin
The JRE is present in the C:/Programfiles/java/jre7
I have a sqljdbc Jar which is placed in the lib folder of the Project in my D drive.
I am using the option "Package Required libraries into generated JAR" but still I think the JAR is not able to fine the correct libraries when I am double clicking on teh JAR in a different machine.
Please suggest. Thanks for all your help.
I am getting UnsupportedClassVersionError in the cmd when I am trying to run the JAR through cmd in other machines
This means that you are using a lower version of the JRE than the JDK which you used to compile the sources (for example, sources compiled with JDK7 can not be executed by a Java 6 or lower JRE since the class file format and hence the class version has changed).
I am also getting unsupported major.minor version 51.0
51.0 is the class file version of JDK 7. Again, this indicates that you try to run your code with Java version 6 or lower.
To verify, check with
C:> java -version
which java version is actually used by default.
To fix this issue, use an absolute path to the java version you require, like
C:> C:\Programfiles\java\jre7\bin\java -jar myapplication.jar
Related
This question already has answers here:
How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor version
(51 answers)
Closed last year.
I am trying to run a Spring Boot app in IntelliJ.
But when I try to run I get the following error message:
Exception in thread "main" java.lang.UnsupportedClassVersionError:
myProject
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
Here is the JRE location under my Run Configuration:
I have tried installing a couple of different Java versions here:
Here are my current system / environment variables:
Environment Variables:
JAVA_HOME: C:\Program Files\Java\jdk-11.0.12\bin
System Variables:
Path: C:\Program Files\Java\jdk-11.0.12\bin ....
I tried to find a JRE that matched the version, but haven't been able to find it.
Can someone please tell me what changes I need to make so I can successfully run this application locally?
Open IntelliJ on your project and go to File >> Project Structure >> Project and on the option Project SDK Select java 11 and apply the changes.
Intellij Project structure
Edit--------
Then try these options on File >> Settings.
1.Set Gradle JVM Home
Gradle JVM
2.Set the JDK version in the Project module settings
JDK version in the Project module
3.Check the JDK version in the Modules
version in the Modules
Close IntelliJ and open it again.
If you are trying to execute the code outside your IDE...
The problem is Windows....
Open your command prompt and type "where java". When you do this, you will notice a "weird" path that looks something like this
C:\Program Files\Common Files\Oracle\Java\javapath\java.exe
I can't recall what Windows version introduced this shortcut, but I want to say it was Windows 7. The problem is that this shortcut points to a Runtime Environment that is more recent (newer) than the Java version that was used to compile the code. In other words, Java is backwards compatible; not forward compatible. Compiled code cannot run in a newer Java version.
How do you fix this?
One way is to go to your SYSTEM Environment Variables and edit the Path system variable and add JAVA_HOME at the very beginning of the paths. It will be something like this
%JAVA_HOME%; {YOUR OTHER PATHS HERE}
If you are trying to execute within the IDE, the problem is similar. POTENTIALLY, you can have a conflict between your PROJECT properties and your global IDE properties, where the Runtime Environment points to a newer version of Java than the JDK used to compile. That said...
I have a very weird issue with an application I worked on years ago. It was a client/server application and a coworker of mine was having this issue because, unbeknownst to her, there was version of the server running in the background that was causing the conflict. So, if you have a similar type of application, check your processes using Task Manager and make sure you either 1) shutdown this application and try again, or 2) Uninstall that application and reinstall the new one. In her case, she tried rebooting her PC but that didn't work because the process would start automatically after each reboot.
I have a jar file that runs fine from the Windows 10 command line, but is not working on my desktop Ubuntu 18.04 command line. The file was exported from Eclipse as a jar, and copied to Ubuntu. The project has about 30 classes over a half dozen packages. I've done some renaming in order to simplify things for this question.
This is the command used to run the jar:
java -jar myproject.jar
On Ubuntu, I get the error "Could not find or load main class com.a.b.LaunchThis"
Following are some of the things I've tried, based on suggestions from many similar posts. Sorry if this is a duplicate, but I couldn't find a workable answer.
I verified that the current Java is 1.8 using the commands:
$ update-alternatives --config java
and
$ java -version
openjdk version "1.8.0_252"
OpenJDK Runtime Environment (build 1.8.0_252-8u252-b09-1~18.04-b09)
OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)
(I also verified that the program runs using java 8 on Windows.)
I ran the following check, which displays all the files, and confirmed that com/a/b/LaunchThis.class exists and is spelled exactly the same way.
java tj myproject.jar
The manifest seems correct.
Manifest-Version: 1.0
Main-Class: com.a.b.LaunchThis
I also tried unpacking the jar and running the program directly from the folder /myproject which contains both /com and META-INF. That should work, shouldn't it?
java com.a.b.LaunchThis
java com/a/b/LaunchThis
java com.a.b/LaunchThis
And from one level outside the myproject folder:
java myproject/com/a/b/LaunchThis
java myproject.com.a.b/LaunchThis
java myproject.com.a.b.LaunchThis
java myproject/com.a.b.LaunchThis
I am not totally clear on when the syntax requires . or /. The package command in "LaunchThis" which holds the main entry point is the following:
package com.a.b;
And in each of the above iterations I also tried including "-cp myproject.jar" as an option.
Always the exact same error message.
To be careful about not having a typo in the above, I first ran the ls command to make sure the file could be found (then edited this line to create the variants).
ls myproject.com.a.b.LaunchThis.class
Any suggestions as to what else to try?
For grins, running with Java11 gets this response:
Error: Could not find or load main class com.a.b.LaunchThis
Caused by: java.lang.NoClassDefFoundError: javafx/event/EventHandler
This makes some sense, because Java11 does not include JavaFX. But Java 8 does include EventHandler. It's been a part of Java since JavaFX 2.
UPDATE: I made two "Hello world" jars, one with and one without JavaFX on an Ubuntu installation of Eclipse running OpenJDK 8. To get the version with JavaFX to run, I downloaded the Oracle JDK 1.8, and linked to /lib/etc/jfxrt.jar as an "external jar" library. Both programs run in Eclipse but only the non-FX jar works. The FX jar gives exactly the same error message.
I think this pretty much establishes my problem to be one of not having JavaFX as part of OpenJDK 8.
My plan is to now try out two solutions: (1) copying the Oracle jfxrt.jar into the OpenJDK lib, (2) backloading OpenJFX 8.
Simply installing OpenJFX from repository loads OpenJFX 11, which is a little dated but will work with the OpenJDK 11.
The issue here is that the repository OpenJDK 8 on Ubuntu 18.04 does not include JavaFX. I'm guessing that the error message results from the fact that a JavaFX main() is located on a class that extends the JavaFX class Application.
How to fix this?
There are a number of things I tested.
First, one could use Oracle's JDK 1.8 instead. I ruled this out due to licensing issues.
Some sites suggested copying the jfxrt.jar from either the Oracle JDK 1.8 or from a working Java program that uses JavaFX and uses a self-contained JRE. I found that if I linked to the Oracle jfxrt.jar (located in the /jre/lib/ext folder) as an external jar, I could run a simple, "Hello world" javafx program in Eclipse. But I could not run the program after it was exported to a jar.
One answer on a related stackoverflow thread suggested copying over several additional files along with jfxrt.jar, which I tried. This did not work, so I'm not going to pass that on.
The solution that has been most successful came from a link provided in the comment by #dave_thomson_085, How do I get Java FX running with OpenJDK 8 on Ubuntu 18.04.2 LTS?.
This answer involves purging and reinstalling openjfx with an older version, and marking it to NOT be subjected to updates.
The code provided by Wolfgang Fahl (which he credits to Druidefix) follows:
apt purge openjfx
apt install openjfx=8u161-b12-1ubuntu2 libopenjfx-jni=8u161-b12-1ubuntu2 libopenjfx-java=8u161-b12-1ubuntu2
apt-mark hold openjfx libopenjfx-jni libopenjfx-java
Now, I can run and test jars containing Java8 using JavaFX that were built on my Windows Eclipse, on my Ubuntu desktop.
I do get a warning message which I haven't dealt with yet:
Gtk-Message: 13:25:40.829: Failed to load module "canberra-gtk-module"
But this is not preventing my programs from running.
I have created an executable JAR file developed on Java version 8. The JAR file was opening on double click. But as the Oracle applications support only Java 6, I had to install JRE 6, but then after the JRE 6 installation, my executable JAR file is not opening.
I have set the JDK 8 bin path in Path environment variables. Is there a solution for this problem? Why is the JAR file not opening after two Java versions in the system?
JAR should open even if two versions 6 and 8 of Java are installed in the system.
You are facing a backward compatibility problem. Backwards compatibility means that you can run a Java 6 program on a Java 8 runtime, but not the other way around.
You can run a lower configuration on a higher configuration, not vice-versa
There are several reasons for that:
Bytecode is versioned and the JVM checks if it supports the version it finds in .class files.
Some language constructs cannot be expressed in previous versions of bytecode.
There are new classes and methods in newer JREs which won't work with older ones.
If you really, really want (tip: you don't), you can force the compiler to treat the source as one version of Java and emit bytecode for another, using something like this:
javac -source 1.8 -target 1.6 MyClass.java
You can compile your code to Java 1.6 bytecode using JDK 1.8. Just take care of the following:
-source=1.8 and -target=1.6 compiler options
If you use Maven, consider having two pom.xml files, with an optional parent file.
Source: Can program developed with Java 8 be run on Java 7?
I am not sure if this solution going to work or not.
Try to run command java -version and look if it returns java 6 or 8 path. Also try to give path of JDK 8 as JAVA_HOME variable and add that into path like this path=%JAVA_HOME%/bin and see if it works. If you get the java 6 as java version try to use above method and then install JRE 6
Hi All Thank you for your response. I kept java6 and reinstalled java8 and now forms and jar both are working!.
In the short term,
the answer is yes. Since both JDK files are downloaded as jar fils it will ok to download both jar files. The reason to not opening after two java versions is as #Elliott said: "in the system is Java 6 can't run Java 8 compiled code, you should be getting an error." That's exactly true but the problem is how to use multiple versions of JDK in a single machine.
Then we have to move on to long term,
The tricky thing is to manage these multiple JDKs and IDEs. It’s a piece of cake if I just use Eclipse for compiling my code because the IDE allows me to configure multiple versions of Java runtime. Unfortunately (or fortunately), I have to use the command line/shell to build my code. So, it is important that I have the right version of JDK present in the PATH and other related environment variables (such as JAVA_HOME).
Manually modifying the environment variables every time I want to switch between JDKs, isn’t a happy task. But, thanks to Windows Powershell, I’m able to write a script that can do the heavy lifting for me.
Basically, what you want to achieve is to set the PATH variable to add the Java bin folder and set the JAVA_HOME environment variable and then launch the correct Eclipse IDE. And, I want to do this with a single command. Let’s do it.
Open a Windows Powershell.
I prefer writing custom Windows scripts in my profile file so that it is available to run whenever I open the shell. To edit the profile, run this command: notepad.exe $profile - the $profile is a special variable that points to your profile file.
Write the below script in the profile file and save it.
function myIDE{ $env:Path = “C:vraajavajdk7bin;” $env:JAVA_HOME = “C:vraajavajdk7” C:vraaideeclipseeclipse set-location C:vraaworkspacemyproject play }
function officeIDE{
$env:Path = "C:vraajavajdk6bin;"
$env:JAVA_HOME = "C:vraajavajdk6"
C:officeeclipseeclipse
}
Close and restart the Powershell.
Now you can issue the command myIDE which will set the proper PATH and environment variables and then launch the Eclipse IDE.
As you can see, there are two functions with different configurations. Just call the function name that you want to launch from the Powershell command line (myIDE).
If any issue please put a comment below!
I just recently recovered some programs from my old hard drive, so I tried compiling one and running it, and I get this error in command prompt:
Unsupported major.minor version 52.0
I compiled the program with Java 8, and I ran it with Java 8. The only thing I can think of is the fact that I used Java 7 with the program on my old computer. Also, I'm using Windows XP on this new computer, while I used Windows 7 on the old computer.
I am using two libraries, but I don't think that is the issue since the program ran fine in NetBeans.
Any ideas of what the problem is?
Based on your comment to archetype's answer, my guess is that java 7 is in one of your windows directories, like C:\Windows\System32 and that directory is before your JAVA_HOME directory in your path. Try typing 'where java' on the CMD. If it's in more than one directory, see which one comes first in your PATH variable and look at the version of java in that directory.
The root cause of the problem is that, the code was compiled in later version but your default runtime is running on the lower version. you can do this by simply checking your current version. java -version and javac -version. You may need to check the Java from the Control Panel. To verify the actual version of your JAR file, you may need to extract the actual version and look for the version information
javap -verbose <Java_Class_Name> | findstr "major"
I get the error below when trying to run an executable jar file. The file only contains a main with one line "System.out.println("Woo!");" and runs fine in eclipse.
I'm not sure how to read these error messages.
(too new to post images correctly)
The error means that you have compiled the code with a higher version of java and trying to run it on a older version of jvm.
Since it says major.minor version 51.0 it means that your compiler is java6. So you should run your program on java6 or higher.
Your compiled class is in jre 1.5 and your system default jre is different(might be 1.6).
compile your code in same version of jre.