How to write a batch file to set classpath and execute java programs
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25176153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write a batch file to set classpath and execute java programs
提问by venky
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.
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.
回答by Mofi
There is no need for a batch file to specify the classpath for Java on command line as Hymanwrote in his comment.
There is no need for a batch file to specify the classpath for Java on command line as Hymanwrote in his comment.
Take a look on the version 7 Java documentation pages:
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.
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.
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.
And double quotes around all paths must be used if one path contains a space character.
Example:
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.
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
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
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).
defined in the properties of the shortcut file (*.lnk).
The working directory can be also defined with Start inin properties of the shortcut file instead of being set in batch file using change directory command.
The working directory can be also defined with Start inin properties of the shortcut file instead of being set in batch file using change directory command.
And an appropriate Commentshould 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.
And an appropriate Commentshould 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 CLASSPATHused by Java executed from within this console window and then remains open for user input.
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 CLASSPATHused by Java executed from within this console window and then remains open for user input.
回答by Yash
Batch file: In order to run class file which uses classes of Some jar file and classes of same application.
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.
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
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.11\junit-4.11.jar;%REPO%\com\oracle\ojdbc5.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 command line option to debug remotely
java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8000 <YourAppName>
回答by Barny
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:
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
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
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:
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
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/
Most code is taken from https://alvinalexander.com/blog/post/page-1/thu-mar-9-2006-dynamically-build-environment-variables-in-dos-c/