5

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?

Eitan T
  • 32,037
  • 13
  • 65
  • 103
User 4.5.5
  • 1,281
  • 3
  • 12
  • 20
  • 1
    I think that here you'll find some more hints http://stackoverflow.com/questions/1914493/add-jar-file-to-buildpath-in-windows-command-line – BigMike Jun 21 '12 at 07:29
  • and btw, good old windows batch FOR command can be usefull too (see http://stackoverflow.com/questions/180741/how-to-do-something-to-each-file-in-a-directory-with-a-batch-script) – BigMike Jun 21 '12 at 07:35
  • @BigMike good point about the java version. In linux systems it's less of a problem due to file name expansion. Not sure windows does some expansion as well +1 – Morfic Jun 21 '12 at 07:55
  • @Grove and ofc on unix alike systems the for command makes much much more sense ;) – BigMike Jun 21 '12 at 07:57

4 Answers4

2

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.

Morfic
  • 14,178
  • 3
  • 40
  • 55
  • Java will read the %CLASSPATH% environment variable anyway, won't it? – trojanfoe Jun 21 '12 at 07:25
  • 1
    You are correct, it should read it automatically. But because setting the classpath can be tricky, it is suggested as a best practice to use the `java -cp` option. This also allows you to specify the classpath individually for each of your apps without affecting the rest of the existing applications, whereas modifying the environment variable will have a global impact. – Morfic Jun 21 '12 at 07:36
  • agree with Grove, messing with env variables on Windows can be a big pain (aaah I miss good old autoexec.bat) – BigMike Jun 21 '12 at 07:58
2

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
guanxiaohua2k6
  • 361
  • 1
  • 5
1

You could try something that is given in the below link http://docs.oracle.com/javase/6/docs/technotes/tools/windows/classpath.html

Understanding class path wildcards section:

"A class path entry that contains * will not match class files. To match both classes and JAR files in a single directory foo, use either foo;foo/* or foo/;foo. The order chosen determines whether the classes and resources in foo are loaded before JAR files in foo, or vice versa. Subdirectories are not searched recursively. For example, foo/ looks for JAR files only in foo, not in foo/bar, foo/baz, etc."

So in your case you should do :

set CLASSPATH=%CLASSPATH%.;.\Jars;.\Jars\*

and not

set CLASSPATH=%CLASSPATH%.;.\Jars;.\Jars\*.jar

provided all the jars you require are present in .\Jars folder

0

set CLASSPATH=%CLASSPATH%.;C:\data\Ideas\tool\Deliverables\webservice\batchjar;C:\data\Ideas\tool\Deliverables\webservice\batchjar*

this helps me