How to force a jar to run in java 7? - java

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.

Related

Can we install two versions of Java JDK on Windows?

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!

Java -version prints a wrong version

I have multiple versions of Java installed in the environment(I know it is bad). I've set the JAVA_HOME to jdk 1_4 directory. This is not installed, but extracted from zip and placed it somewhere. We have java 1.8 JRE also installed in the system. I never set the path to this installed directory. Now i'm trying to run some ant script that depends on jdk 1_4. I get some exception saying that it is not able to find tools.jar in java1.8... .
My question is that when the path and java_home are set to jdk 14 why does the ant look for 1.8 version of java? I'm confused.
Update: I have looked up the duplicate issue.
Additional details: Java -version prints 1.8 with jdk 14 in the path and java_home. I uninstalled 1.8. reopened cmd tried java -version, now it errors out saying that it is not able to find java1.8 instead of trying to find the next java available in the path or java_home.
Error shown:
C:\Users\usrpao>java
Error: could not open `C:\Program Files\Java\jre8\lib\amd64\jvm.cfg'
I have multiple versions of Java installed in the environment(I know it is bad). I've set the JAVA_HOME to jdk 1_4 directory.
Not wrong at all. I have a similar setup because I have to switch between Java version.
First, install Java JDKs in the root of your drive. No spaces in the directory names. For example, C:\Java\Java-1.6-21 and C:\Java\Java-1.7-5.
In your Environment Variables section in your System Control Panel (under Advance), create an environment variable to point to each one of these Java Home directories. For example, JAVA_HOME_17 = C:\Java\Java-1.7.5 and JAVA_HOME_16 = C:\Java\Java-1.6-21. This should be a System Environment variable.
Create a JAVA_HOME environment variable that points to the Java version you want: JAVA_HOME = %JAVA_HOME_17%.
Now in the System PATH, prefix the PATH (the very first entry) with %JAVA_HOME%\bin.
When you open a console window, your default java and javac commands will be the correct Java version.
If you need to change a version, change the JAVA_HOME environment variable to point to the correct environment variable and open a new console window. Now that new Java will be in your path.
NOTE: It is vitally important that %JAVA_HOME%\bin is in the first part of your PATH before C:\Windows\System32. You don't want the java.exe that exists in that directory to be your default java.
You haven't specified the operating system, so:
on windows, java installs a java.exe in C:\Windows\system32, which is probably on the path before java_home, and so gets picked up
on Linux, various distributions that support multiple installed java versions will also have some symlink earlier on the path. Run 'which java' to determine where that symlink is
Multiple java versions in the system is the problem, I uninstalled all the java versions first. Restarted my machine. Path and java home points to the jdk 1.4. java -version now points to the version specified in the path. Thanks all.

Why system variable (path) wasn't created after jdk 1.7 installed

All, It was the the first time I met this kind of problem, When I installed the jdk 1.7, there is no problem found in this process. After that , I try to verify the version of the jdk . so I run the java command in the dos window. But got the result says java is not recognized as an internal or external command, operable program or batch file. Can anybody tell me why and what happen? thanks.
The Java installer does not automatically add the bin directory of the JDK to the PATH, as you seem to expect.
You'll have to do that yourself, as is explained in the JDK installation instructions:
Updating the PATH Environment Variable (Optional)
You have to set the Path variable under Start -> System to the bin folder of your java installation if you are working under Windows

JAVA_HOME and java -version

I'm using a Windows .bat script and I set JAVA_HOME as C:/Program Files/Java/jdk1.6.0_32
when I do a java -version, it still shows the 1.3
How can I fix this? What am I doing wrong?
For me the issue was in my PATH variable, C:\ProgramData\Oracle\Java\javapath; was added by java windows install before my %JAVA_HOME%\bin;.
So I'd echo %JAVA_HOME% pointing to a JDK7 and java -version showing jdk8.
I'd to put %JAVA_HOME%\bin; before C:\ProgramData\Oracle\Java\javapath; so that java -version displays jdk7.
Try %JAVA_HOME%\bin\java -version
If you modify JAVA_HOME, it's usually better to invoke java with an absolute path (using JAVA_HOME) because the new binary is probably not in the path (and then Windows will load the wrong binary).
Make sure that the PATH environment variable is pointing to %JAVA_HOME%\bin.
Be sure not to mix the system variable path and the user variable system path. I feel OK in calling java without the absolute path (when I know how JAVA_HOME and PATH are configured).
Calling java -version from command line, causes cmd.exe to do the lookup on the "known" directories. "Known" means PATH environment variable. It seems that your PATH contains a java 1.3 bin folder, and not 1.6.
JAVA_HOME is another variable, that is used (for example, and not only) by java wrappers, or by scripts executing some java stuff.
Try doing this:
SET JAVA_HOME=C:/Program Files/Java/jdk1.6.0_32
%JAVA_HOME%/bin/java -version
Add quotes where needed.
I had similar issue,in my case , I had two versions java installed. it can be fixed by uninstalling one version of java completely from system.
Had a similar scenario today - two Windows 10 devices - both have JRE 1.6 & 1.7.
When typing
Java -version
One device shows 1.6 the other 1.7.
This was preventing me running a third party JAR to install some software on the device showing 1.6 (which worked fine on the device showing 1.7 when running java -version), using:
java -jar ThirdParty.jar
As the JAR needed to be run by 1.7.
Cause of this was in the PATH environment variable - one device had the location of 1.6 first in the PATH list, moving the 1.7 location above the 1.6 location resulted in consistency using Java -version and allowed me to install the software.
java -version will consult the paths in the special environment variable Path. You need to select the java version you want and move it upwards the latter (click "Move Up"). You probably have that reference to JDK 1.3 in Path above your addition of JDK 1.6. Since that's the first thing the OS finds, that's what it chooses to run.
Executing the command again with the same window opened after changing the environment variables will not work. Re-open it
I know this question is old but this was my case and I wanted to re-explain further, similar to #DanBot 's case

What is JAVA_HOME? How does the JVM find the javac path stored in JAVA_HOME?

I would like to know what JAVA_HOME is. Where do I set the path of javac.exe and java.exe. It is in environment variables? When I compile a Java program from command prompt, how does the JVM find javac.exe?
JVM does not find java.exe. It doesn't even call it. java.exe is called by the operating system (Windows in this case).
JAVA_HOME is just a convention, usually used by Tomcat, other Java EE app servers and build tools such as Gradle to find where Java lives.
The important thing from your point of view is that the Java /bin directory be on your PATH so Windows can find the .exe tools that ship with the JDK: javac.exe, java.exe, jar.exe, etc.
JAVA_HOME and JRE_HOME are not used by Java itself. Some third-party programs (for example Apache Tomcat) expect one of these environment variables to be set to the installation directory of the JDK or JRE. If you are not using software that requires them, you do not need to set JAVA_HOME and JRE_HOME.
PATH is an environment variable used by the operating system (Windows, Mac OS X, Linux) where it will look for native executable programs to run. You should add the bin subdirectory of your JDK installation directory to the PATH, so that you can use the javac and java commands and other JDK tools in a command prompt window. Courtesy: coderanch
set environment variable
JAVA_HOME=C:\Program Files\Java\jdk1.6.0_24
classpath=C:\Program Files\Java\jdk1.6.0_24\lib\tools.jar
path=C:\Program Files\Java\jdk1.6.0_24\bin
The command prompt wouldn't use JAVA_HOME to find javac.exe, it would use PATH.
JAVA_HOME is a environment variable (in Unix terminologies), or a PATH variable (in Windows terminology). A lot of well behaving Java applications (which need the JDK/JRE) to run, looks up the JAVA_HOME variable for the location where the Java compiler/interpreter may be found.
JAVA HOME is used for setting up the environment variable for JAVA. It means that you are providing a path for compiling a JAVA program and also running the same. So, if you do not set the JAVA HOME( PATH ) and try to run a java or any dependent program in the command prompt.
You will deal with an error as
javac : not recognized as internal or external command.
Now to set this, Just open your Java jdk then open bin folder then copy the PATH of that bin folder.
Now, go to My computer right click on it----> select properties-----> select Advanced system settings----->Click on Environment Variables------>select New----->give a name in the text box Variable Name and then paste the path in Value.
That's All!!
JAVA_HOME is an environment variable which is read by some development tools like Apache Tomcat, Apache Maven, Gradle, Jenkins etc. Usually, JAVA_HOME is set to point to JDK instead of JRE because these development tools need to use tools like compiler, debugger, document generator etc. which are only available in JDK since JDK is a development kit. JRE is only meant for running java applications. JDK = JRE + Development tools
Consumer facing Java applications don't read JAVA_HOME variable and they just need to know where the JVM is located and that's why JVM location (directory) needs to be added to the PATH variable. This is automatically done for you when you install Java software. Whenever you try to run a Java application by either double clicking on the app or through command line, your operating system reads PATH variable to locate and run JVM and it doesn't look for JAVA_HOME variable.
use this command /usr/libexec/java_home to check the JAVA_HOME
JAVA_HOME is an Environment Variable set to the location of the Java directory on your computer.
PATH is an internal DOS command that finds the /bin directory of the version of Java that you are using. Usually they are the same, except that the PATH entry ends with /bin
Basically JAVA_HOME is use to set path of the java . it is use in windows. it's used for set path of the multiple software like as java EE , ANT and Maven.
this is the steps to solve your problem:
only for core java to set path :
path :"C:\Program Files\Java\jre1.8.0_77\bin"
but when you are use multi built like as ANT , core java then you are used JAVE_HOME in environment .
follow the steps :
JAVA_HOME:"C:\Program Files\Java\jre1.8.0_77\bin"
ANT_HOME:"C:\ant\apache-ant-1.9.6"
Path: JAVA_HOME, ANT_HOME;
it is the systematic way to set the environment variable..

Categories

Resources