venky
venky

Reputation: 422

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

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.

Upvotes: 3

Views: 36295

Answers (3)

Barny
Barny

Reputation: 1855

Maybe this answers not your question about closing the batch after executing it, but I use files like this to set my CLASSPATH environment varible:

call-java-class.bat

::
:: %1 directory where all jars are located
:: %2 main class
:: %* program parameters
::
:: JAVA_OPTS ... parameter before main class
::
@echo off

set libdir=%1
shift
set main=%1
shift

set JARS=
set CLASSPATH=

pushd %libdir%
for %%i in (*.jar) do call cpappend.bat %libdir%\%%i
set CLASSPATH=.%JARS%
popd

:: debug
echo %CLASSPATH%
echo %JAVA_HOME%\bin\java %main% %1 %2 %3 %4 %5 %6 %7 %8 %9

%JAVA_HOME%\bin\java %JAVA_OPTS% %main% %1 %2 %3 %4 %5 %6 %7 %8 %9

cpappend.bat

@echo off
set JARS=%JARS%;%1

The script expects all dependencies in a single directory and JAVA_HOME must be set. Then call your java program:

call-java-class.bat path-to-lib-dir com.foo.bar.Main -help

Most code is taken from https://alvinalexander.com/blog/post/page-1/thu-mar-9-2006-dynamically-build-environment-variables-in-dos-c/

Upvotes: 0

Yash
Yash

Reputation: 9568

Batch file: In order to run class file which uses classes of Some jar file and classes of same application.

As of Eclipse IDE Set all build path entries to the Class Path. As we are running it through batch file we need to set the target application class file too. As shown below.

REM MyApplication\mainClass.bat
REM MyApplication\target\classes - %~dp0target\classes;

mainClass.bat which contains set class path

@echo off
REM Maven local repository path
SET REPO=%USERPROFILE%\.m2\repository

REM Setting the class path
SET CLASSPATH=%REPO%\junit\junit\4.11\junit-4.11.jar;%REPO%\com\oracle\ojdbc5\11.1.0.7.0\ojdbc5-11.1.0.7.0.jar;%~dp0target\classes;%~dp0target\test-classes


java -Xmx256m com.github.yash777.MainClass

Java command line option to debug remotely

java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8000 <YourAppName>

Upvotes: 0

Mofi
Mofi

Reputation: 49096

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.

Upvotes: 1

Related Questions