How to have two different java SDK on path on windows - java

I have a program which seems to only work with a later version of java (14.1.03), however I don't want to uninstall my current up-to-date java version. I was wondering if it's possible to have 2 instances of java in the path on windows 10 and be able to compile programs through the command prompt with choice on which instance of java has compiled the program.
Thanks in advance!

Basically, putting multiple Java installation directories onto %PATH% won't work.
The %PATH% variable is a list of directories for the command processor to search to resolve command names. When you tell the shell to run java, it looks at each directory in turn until it finds an executable command that matches java (e.g. java.exe, java.bat, etc). Then it stops looking.
So if you have Java 11 and Java 14 on the %PATH%, one of them will be earlier the list than the other. The Java commands will come from that Java install, and the other install will (typically) be ignored.
Solutions:
One approach is to set %PATH% according to the Java install that you want to use right now:
You could do this by embedding a local %PATH% setting in the batch file you use to launch your application.
You could do it interactively
You could do it by having user (or account) specific %PATH% settings.
Another approach is to use absolute command names; e.g. C:/JDK11/bin/java or C:/JDK14/bin/java instead of java. (Use appropriate paths of course.)
(On Linux, there is a nice tool called alternatives that can be used for managing and switching default commands. It does it using symlinks ...)

Related

What is the best way to deal with the project having multiple jdk compiled dependencies?

I have an ant based project which was build with Java 5, its an old project.
Now I need to work on that again to make some changes in it. I have this other project jar, which is build with Java 7 , and this jar needs to be added as dependency to the old project.
As per my understanding, I have two possibilities here:
Build that old project with Java 7
Build New project with Java 5
The 2nd options is near to impossible, as there are lot of class structure changes since Java 5, I have to rewrite the code almost from scratch.
The first option seems doable, but what do I need to take care of in order to build a Java 5 project with Java 7, as we know there are class structural changes.
Note: I have tried building this old project with Java 7, it says
"cannot access java.lang.Object bad class file: class file has wrong version 51.0, should be 49.0"
I have had a similar issue, and I have discovered I had different java versions installed. Since I needed both, I made a script to change JAVA_HOME path on the fly.
Assuming you are on a Windows machine, firstly you need to set up enviroment variables:
No Java-related entries in the top list ("User variables")
JAVA_HOME = C:<Path to java installation> in the lower list ("System variables")
Under "Path" substitute C:\ProgramData\Oracle\Java\javapathand C:\Program Files (x86)\Common Files\Oracle\Java\javapath with %JAVA_HOME%\bin
Also add C:\Program Files\Java\scripts (assuming you want to place your scripts there) in order to use shortcuts to change running Java
Assuming you want a shortcut to Java7, you should create a java7.bat file and edit like this
#echo off
set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_79
set Path=%JAVA_HOME%\bin;%Path%
echo Java 7 activated.
Be careful to match java path to the actual path on your machine.
Make as many .bat files as needed (e.g. java5.bat, java7.bat) and simply type java7 in cmd to change version.

Convert jar to exe

I try to convert a jar file to an exe file.
I followed the instructions provided here using launch4j .
I get only an XML file but no exe as expected.
Any ideas?
So how should I deploy my application?
Your user would have to have the JRE installed on their computer. However, you could also distribute java.exe and rt.jar (the java run time jar) as well as any library jars you need for your application along with your program. Since the java command has an optional classpath parameter you could write a batch file (something like runme.bat) which would look something like this:
java -cp rt.jar;myapplication.jar;otherjars.jar MyMainClass
This is still Windows specific because the java.exe file would be a Windows specific file. You could also add in logic to check for the environment variable JAVA_HOME and if that's set on the user's computer then the java program specific to their OS would be in JAVA_HOME/bin
Your real problem comes when you're not sure what operating system your user will be using AND you don't know if they have Java installed. If you simply require that the user has the Java Runtime Environment installed then you're problem is solved for all users.
I hope that helps.
Greg

Deploying basic java app for distribution

I created a java text based game. I exported it as a runnable jar file in eclipse, but double clicking didn't work to run it (not sure why this is, would appreciate an explanation) so I created a .bat file which has:
java -jar game.jar
This works on some computers. However, when a user who doesn't have java in their PATH (I assume this is the reason) runs the .bat file, it comes up with the error:
java is not recognized as an internal or external command
How can I make my java game able to run on all computers that have java (1.6) installed?
java -jar <your-jar> is, I think, the best you can do, if you want to support different operating systems as well. If a user doesn't have java command in the path, java support has not been installed as a public JRE and user should either fix the configuration or explicitly specify which private JRE is to be used. Oracle documentation explicitly states about the public JRE that
You must set the PATH environment variable to point to JAVA_HOME\bin
(where JAVA_HOME is the location where you installed the public JRE)
to register the JRE.
so a user should do just that. You can, however, check if JAVA_HOME is set and if java exists in the path and provide informational error message if it is not.
If you're fine with only supporting windows, you can use various .exe packaging systems to ease the process for end user. See this, this and this thread for details.

Installing Java in Win7 64bit

I have a new laptop and attempted to install jdk-1_5_0_22-windows-amd64, jdk-1_5_0_22-windows-i586-p and jdk-6u22-windows-i586 without success.
I have used java before and I am familiar with environment variables.
However I cannot get the java version at command prompt or the java compiler to work.
Also I am using Blackberry JDE and it does not find the installed version to build.
Can anyone help me?
Add the path to the Java bin folder to your PATH environment variable.
In order to run any java from the command line, the bin folder of the java installation must be added to the PATH environment variable. Without this, the command prompt cannot find your java installation, so it cannot run your files. Make sure to open a new copy of the command line after you set the environment variable.
Right click on My Computer and choose Properties. Choose the Advanced tab, and then the Environment Variables button. There should be a "Path" under system variables. Add a semicolon to the end and add the path to your installation's bin folder after that.
I would imagine that the Blackberry JDE makes use of the JAVA_HOME variable. I am guessing that it is not set so the JDE doesnt know where to look on the file system.
I think that Erick is correct in verifying that the bin directory has been added to the PATH. If you used the installer, you might need to reboot your machine.
Finally, the JDE appears to be a slightly aged package. I cannot seem to find the statement saying that the Blackberry JDE supports 64 bit java.

How to export all installed versions of Java to a text file under Windows XP

I'm looking to write a batch file I can deploy to 20-30 users that will export all of the enabled versions of Java out to a text file, which I can then pick up, and add to a troubleshooting effort we're working on.
I know that java -version will show the current version, but we're in an environment where we have multiple applications that use different versions.
Thanks,
Josh
Assuming your runtimes are all in the same folder,
You can grab the "JAVA_HOME" environment variable then just print a list of the folders in the parent directory of wherever it points.
However individual applications can bundle their own runtimes, which can be anywhere pretty much.
Since you reference -version though, I assume you are using it from the command line so pulling in JAVA_HOME and looking around there seems like a good place to start.
Have a look at the Java Deployment Tookit with its method getJREs(). This returns an array of currently installed Java Runtime Environments.
Please be advised, that this a JavaScript and you probably won't be able to use it from command line.

Categories