File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes How to set JAVA_HOME Dynamically? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to set JAVA_HOME Dynamically?" Watch "How to set JAVA_HOME Dynamically?" New topic

How to set JAVA_HOME Dynamically?

S. Vijay
Ranch Hand

Joined: Jul 10, 2009
Posts: 44
Hi Everyone

I am trying to set JAVA_HOME dynamically. I am trying to set the value in the registry but that is not getting refreshed. For example my scenario is this

1) Run a java Program
2) User selects one java version out of many available java versions
3) User clicks a button which internally takes the input and changes the java version.

I tried doing the following code snippet

On doing the above I could able to change the registry value but when I run java applications it is still using the previous java version not the changed one.

Can anyone help me how to change the java version by using the registry or by any other means?

Please note that setx command cannot be used in my machine since mine is Windows XP so it would be very helpful if anyone help me to provide a solution other than what mentioned.

James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Neither java nor javac take any notice of JAVA_HOME . Some applications such as Tomcat use JAVA_HOME in their startup scripts but Java does not use JAVA_HOME.

Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

S. Vijay wrote:
mine is Windows XP so it would be very helpful if anyone help me to provide a solution other than what mentioned.

hmm. good even I am using xp. as james said JAVA_HOME is used by some other application needs to find out where is java has been installed.if you want more specific answer then elaborate.
S. Vijay
Ranch Hand

Joined: Jul 10, 2009
Posts: 44
Ok I will explain in detail what I am trying to do

Please see the attachment image

When I change the Java home version then my jdk version gets changed if I use the Environment Variables from System Properties windows GUI.

For example if I am using JDK1.5 and if I change the JAVA_HOME version to JDK1.6 then the java version changes to the corresponding 1.6 version.

So in my application I created a combo box which displays the different java version and when the user selects the version of his choice and click the change button then my application has to change the java version to the corresponding version.

I tried the code which was mentioned in my first post. This makes change to the registry variable and I can see the corresponding changes in the environment variable settings but still I see previous java version used not the changed one.

Please let me know if I am not clear.

[Thumbnail for img.JPG]

Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

May be the issue is that you need to close and reopen the command window to see the new changes (assuming the environment variable has really changes)

You can try changing JAVA_HOME from one command window and see that changes are not reflected in the other command window.

Java Experience
I agree. Here's the link:
subject: How to set JAVA_HOME Dynamically?
It's not a secret anymore!