This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have 2 versions of Java on my machine. 1.3.1_13 which I use at my place of employment, and 1.5.0_07 which I am using for the SCJP CX-310-055 Exam. Rather than manually set/reset the pertinent environment variable to switch back and forth from the 2 versions, I'm trying to write a batch file to handle this for me. This should be a straightforward thing to do, but I have been unsuccessful up to this point. Below is the batch file I have created:
Since this is for personal use, I have not taken any defensive coding measures, so just assume the user will always type either 'setjavaversion old' or 'setjavaversion new'.
Any help is greatly appreciated. Thanks!
setjavaversion.cmd ================== @echo off REM setjavaversion.cmd REM REM J. Mickley REM 01/31/2007 REM REM A batch file that sets java-specific environment REM variables. Namely JAVA_HOME which is used in REM CLASSPATH and PATH.
:new SET JAVA_HOME=C:\Program Files\Java\jdk1.5.0_07 GOTO end
ld SET JAVA_HOME=C:\Program Files\Java\jdk1.3.1_13 GOTO end
:end ECHO JAVA_HOME environment variable set to: ECHO %JAVA_HOME%
Joined: Jan 31, 2007
haha! the ld line should be read as
Sorry 'bout that
Joined: Jan 31, 2007
arg, I give up :-)
It is meant to be a colon, followed by the word 'old' to act as a label
The batch file seems to behave as expected within the command prompt. However, I do not see this change reflected when I right-click on 'My Computer' and view environment variables. Also, when I exit the command prompt, the setting is not persistent.
I am looking for a way to make this change remain in effect beyond the lifetime of the command prompt process.
The environment variables that you set in a batch file are indeed not persisted on the user or system level. They are only set for the command prompt that the batch file runs in. You can't change the user or system environment variables by setting environment variables in a batch file, and I don't know of any other way to do this from a batch file.
The Windows API probably has a way to access the user and system level variables - you'd have to write a program in C, C++, C#, Visual Basic or something else from which you can call those Windows API functions.
By the way, normally you do not need to set the CLASSPATH environment variable. If you don't set it, Java will look in the current directory for class files. You certainly don't have to include %JAVA_HOME%\lib in the CLASSPATH. Also, the JDK does not need JAVA_HOME to be set.
The only thing you need to do to have a different Java version as the one that starts up if you type "java" in a command prompt window is set the PATH variable.
But even that is not really necessary, you can also just specify the complete path of the java.exe that you want to run, for example:
C:\Java\jdk1.5.0_10\bin\java.exe MyClass [ February 02, 2007: Message edited by: Jesper Young ]