How to write a batch file to set classpath and execute java programs

31,059

There is no need for a batch file to specify the classpath for Java on command line as Jack wrote in his comment.

Take a look on the version 7 Java documentation pages:

There is -cp or better readable for humans -classpath which can be used on command line to define the classpath.

The paths to multiple classes can be specified by using a semi-colon as separator.

And double quotes around all paths must be used if one path contains a space character.

Example:

"%JAVA_HOME%\bin\java.exe" -classpath "C:\Java\My First Class;C:\Java\MySecondClass" MyJavaApp

This approach is mainly useful on using a shortcut (*.lnk) to a Java application which needs different classses than what is usually used and defined in system wide environment variable CLASSPATH.

But for developing and testing Java applications in a console window with a different list of classes than defined system wide, it is better to have a batch file for example with name JavaDevEnv.bat with following code

@echo off
title Java Development Environment
cd /D "Path to\Preferred Working Directory\For\Java\Development"
set "CLASSPATH=C:\Java\My First Class;C:\Java\MySecondClass"

and create a shortcut on Windows desktop or in Windows start menu with the command line

%SystemRoot%\System32\cmd.exe /K "Path to\Batch File\JavaDevEnv.bat"

defined in the properties of the shortcut file (*.lnk).

The working directory can be also defined with Start in in properties of the shortcut file instead of being set in batch file using change directory command.

And an appropriate Comment should be also written in properties of the shortcut file, for example same as used on command title which sets the title of the console window as hint for which purpose to use this shortcut.

A double click on this shortcut results in opening a new console window, executing the batch file which sets window title, working directory and environment variable CLASSPATH used by Java executed from within this console window and then remains open for user input.

Share:
31,059
venky
Author by

venky

SOreadytohelp

Updated on May 20, 2020

Comments

  • venky
    venky almost 4 years

    Some of my java programs need so many jar files to execute. For executing this, I may have to add all of those jar files in classpath variable of environment variables or else have to set classpath manually at command prompt each and every time when I open a new cmd prompt. I do not want to add all jar files at classpath variable in environment variables and also set manually each and every time when I open new cmd prompt. I would like to write a script in batch file to set classpath and there it self need to run java programs. So that, when ever I want to run my programs, I will just run batch file and run my programs one by one. I have written a batch file to set classpath. But, when I run this batch file, it automatically get closed. So, Am not able to utilize the classpath I set by batch file.Again I had to open new cmd prompt, set classpath and run my java programs. To achieve this with batch file, how could I proceed. Appreciate any help. Thank you.

    • Jack
      Jack over 9 years
      You can add the classpath with the -cp parameter directly with the call to the java executable.