Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to refer all the jar files in a directory in my batch file

I have created a batch file "run.bat":

set CLASSPATH=%CLASSPATH%.;.\Jars\app.jar;.\Jars\a.jar;.\Jars\b.jar;.\Jars\c.jar;.\Jars\d.jar;
java mypackage.mysubpackage.Start
pause

I have kept all the class files related to my application in "app.jar" and Start is the class from where the application begins execution. I have this "run.bat" file and all the jars that my "app.jar" wants to refer in the same directory.
I kept all these jars in the "Jars" folder and referring to it in my "run.bat" file as shown above. However, to refer to each and every jar file by my "run.bat" I need to specify the path as ".\Jars\jarname.jar". When I am using ".\Jars\*.jar" the jars are not referred by "run.bat". Can anyone provide an alternative for it?

like image 679
User 4.5.5 Avatar asked Jun 21 '12 04:06

User 4.5.5


2 Answers

Actually you have done only half the work using *.jar. You also need to pass them to java as the classpath: java -cp $CLASSPATH mypackage.mysubpackage.Start. (on windows I think the use of a variable in a script is %CLASSPATH%)

Later edit: take a look at BigMike's comments on your question. If you're using a java version < 1.6, you might need to use a loop to build a complete %CLASSPATH% including each jar's full name individually, because I'm guessing that Windows' shell doesn't do expansions just like *nix systems.

like image 116
Morfic Avatar answered Sep 25 '22 18:09

Morfic


You can try to use for loop to create class path in batch, such as the below.

@echo off
for %%jar in (.\Jars\*.jar) do call :add_jar %%jar

java -cp %CLASSPATH%;%JARS% mypackage.mysubpackage.Start
pause

exit /b

:add_jar
set JARS=%JARS%;%1
exit /b
like image 34
guanxiaohua2k6 Avatar answered Sep 22 '22 18:09

guanxiaohua2k6