I have a jar file that I created on my PC using Java 7. I want to run it on my lab server which only has Java 6. I try to run the jar file: java -jar file.jar but there was a UnsupportedClassVersionError exception.
Since I have no root right, I cannot install the new Java 7 on the server. However, I can download JDK7 to my working folder (let say /home/jdk7)
Is there a way to run the jar file by specifying the jdk path to /home/jdk7 ?
Thanks.
You can use the java executable from your private JRE/JDK installation explicitly. If it's installed in /home/jdk7/, the full command line should be:
/home/jdk7/bin/java -jar file.jar …
Alternately, you can make this installation's executables the "default" by prepending them to your PATH, by using the following in ~/.bashrc or ~/.profile. (One of those or both will be correct for your lab system, I'm not sure since I use fish.)
export PATH=/home/jdk7/bin:$PATH
Note: this will only help if it's in the environment of the logged in user.
You can't. You can run Java 6 projects on Java 7, but not Java 7 projects on Java 6. This is because you can use some instructions in Java 7 that don't exist in Java 6. I've got this problem sometimes too.
Related
I've just istalled JDK8 on PC Windows 11 Home
Now im trying to run simple default program from NetBeans14
In environmental variables:
PATH : C:\Program Files\Java\jdk1.8.0_202\bin
CLASSPATH : C:\Program Files\Java\jdk1.8.0_202\bin
Project build on hard disk
C:\Projects\Hello\src\main\java\pack\hello
on cmd going tn cd C:\Projects\Hello\src\main\java\pack\hello
javac compiles .java file on .class file
command "java Hello" output: Error: Could not find or load main class Hello
Hello.class is there.
checking commands, all works:
java
javac
javac -version
Please to support, or send some similar problems posts.
All web answers are speaking about setting PATH and CLASSPATH, where in my case it already took place.
Thanks in Advance
I tried to follow your steps. I downloaded and installed the newest JDK 8 on Windows 11, and NetBeans 14.
Then I looked through the Environment Variables, and the only mention of Java was in the system Path variable:
You see, I have no CLASSPATH variable, nor haven't I manually set anything. Everything was installed automatically. I have no explicit path to C:\Program Files\Java\jdk1.8.0_... in my Path variable.
And I created a simple app in NetBeans. It runs from NetBeans successfully. Just do "Run Project" (F6) in the NetBeans interface.
If you need to run it from the command line for some reason, this question may help.
If you need to run it on another computer, I would advise you not to do it with cmd. Compile it into jar, then wrap it into exe (in case of Windows) using launch4J.
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 have made a java application using jdk 8 and then made an executable file from the jar.
Then I updated my system to java 11, and deleted all older java versions from my system, and when I run this app from exe file it can't run and takes me to web browser and asks me to download java 8. But if I run the jar using java -jar app.jar it runs normally on java 11.
I've used launch4j to make an exe out of jar, and there is no possibility to pick java 11 as the newest version.
So how can I make this exe run on java 11?
Unless you have compiled EXE with latest launch4j 3.12 you are most likely stuck on Java 8. Support for newer JDK was added in ticket #177 Launch4J doesn't accept Java 9 JDK.
The solution would be to recompile EXE with latest launch4j and hope it works with Java 11. Alternatively you could create a BAT file running java -jar app.jar but that goes against the whole point of launch4j.
I made a Runnable Jar that will run as service in windows, but the final pc that will execute him, has 3 versions of java installed, 1.3, 1.6 and 1.7, and my jar needs to be executed with 1.7, because him uses classes that are present only in 1.7+ version, o don't know how are configured the environiment variables, my question is:
Is possible to force my code to run in java 1.7?
You can also do this without persistently resetting your path environments. Just use the absolute path to your jre/jdk instead of typing "java": e.g.: "C:\Program Files\Java\jre7\bin"\java -jar foo.jar, same goes for MacOS and Linux.
Set your JAVA_HOME and PATH environment variable to point to the Java 7 directory.
See https://superuser.com/questions/284342/what-are-path-and-other-environment-variables-and-how-can-i-set-or-use-them to know how to do it.
Rather than setting path/classpath at top level, you should create a
batch file and set path/classpath there.
Then command to run your jar.
I am trying to run the jar file on linux created from windows using jdk7 .
I am using the following command to run the
java -jar jarfile.jar
its running the jar and throwing the exception as un recognised class format version. because linux is showing the java version as 1.4.2 while i have extracted the jdk1.7 in the root directory but it is still taking the java version as 1.4.2 . what i have to do to run the jar file created using jdk1.7 . I have extracted the java (jdk1.7) on linux in root directory.
You need to specify the JDK 1.7 on your path e.g.
$ PATH=/jdk17/bin:$PATH
Note how you specify not just the path to the 1.7 installation, but the bin path within that.
Typing
$ which java
after you've changed the PATH will confirm if this has worked.
Make sure jdk1.7 in front of jdk1.4.2 in system variable PATH. Do you have installed oracle in your Linux OS? I have encountered the same question in windows OS because I have installed oracle. If some software just like oracle is installed, maybe it will insert jdk1.4 in front of all other software paths in system variable PATH.