Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting CLASSPATH via batch file

 
Josh Mickley
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

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.

GOTO %1

: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%
 
Josh Mickley
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
haha! the ld line should be read as

Sorry 'bout that
 
Josh Mickley
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arg, I give up :-)

It is meant to be a colon, followed by the word 'old' to act as a label
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Josh Mickley:
arg, I give up :-)

It is meant to be a colon, followed by the word 'old' to act as a label


Check "Disable smilies in this post" to avoid that problem.

What isn't working? So far you've only set JAVA_HOME, not CLASSPATH or PATH.
 
Josh Mickley
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the tip Carol. My CLASSPATH and PATH settings are fine as they are defined in a fashion similar to:

CLASSPATH=.;%JAVA_HOME%\lib;...
PATH=.;%JAVA_HOME%\bin\...

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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic