Tuesday, November 27, 2007

Setting the class path in Windows

set CLASSPATH=classpath1;classpath2...

Classpath entries that are neither directories nor archives (.zip or .jar files) are ignored. If the classpath ends with a file location that should be archives. If the classpath ends with the directory then the *.class found in the path will be added in JVM CLASSPATH

Is there a way to add all the jar files in a folder / directory to CLASSPATH?.

No. There is no way of setting directly the all the jars in folder to the classpath.

set CLASSPATH=%CLASSPATH%;%LIB%/java/classes.jar
set CLASSPATH=%CLASSPATH%;%LIB%/java/rt.jar
set CLASSPATH=%CLASSPATH%;%LIB%/java/junit.jar
set CLASSPATH=%CLASSPATH%;%LIB%/java/tools.jar

Alternate approach is possible by collecting the jars from a folder and setting into CLASSPATH.

set CP=
for %%f in (%LIB%\*.jar) do set CP=!CP!;%%f
echo %CP%
java -cp %CP% yourclassname

Keep in mind:

Here ! means that variables values are expanded later.

Don't Use CLASSPATH instead of CP because it will lead you to expose these setting to environment. If you want to use CLASSPATH then the code has to be placed within setlocal and endlocal.

Delayed environment variable expansion support is always disabled by default, but may be enabled/disabled via the /V command line switch to CMD.EXE. See CMD /?

The above mentioned echo %CP% will display as !CP!;%LIB%/java/tools.jar , but this will be expanded while using in any commands like java, javac.Show text-ads on your Website or Blog with BidVertiser.

Few of Dynamic variables from Windows:

If Command Extensions are enabled, then there are several dynamic environment variables that can be expanded but which don't show up in the list of variables displayed by SET. These variable values are computed dynamically each time the value of the variable is expanded. If the user explicitly defines a variable with one of these names, then that definition will override the dynamic one described below:

%CD%
expands to the current directory string.
%DATE%
expands to current date using same format as DATE command.
%TIME%
expands to current time using same format as TIME command.
%RANDOM%
expands to a random decimal number between 0 and 32767.
%ERRORLEVEL%
expands to the current ERRORLEVEL value
%CMDEXTVERSION%
expands to the current Command Processor Extensions version number.
%CMDCMDLINE%
expands to the original command line that invoked the Command Processor.

No comments:

Post a Comment

Recent Posts

Unix Commands | List all My Posts

Texts

This blog intended to share the knowledge and contribute to JAVA Community such a way that by providing samples and pointing right documents/webpages. We try to give our knowledege level best and no guarantee can be claimed on truth. Copyright and Terms of Policy refer blogspot.com