wood burning stoves 2.0*
The moose likes Java in General and the fly likes Running Java program using .bat file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Running Java program using .bat file." Watch "Running Java program using .bat file." New topic
Author

Running Java program using .bat file.

Preetish Madalia
Ranch Hand

Joined: Dec 12, 2007
Posts: 37
Hi,
I want to run a java using .bat file. I require some .jar files to be included in classpath as well. Can anybody tell me how to write bat file this case. Thanks in advance.
Mirko Bonasorte
Ranch Hand

Joined: May 14, 2007
Posts: 244
java -cp <all your jars, including the one which has the main class> <main class>


SCJP<br />SCWCD 1.4 Upgrade (Remember: me stupid)<br />SCWCD 1.4<br /><a href="http://jcp.org/aboutJava/communityprocess/final/jsr220/index.html" target="_blank" rel="nofollow">SCBCD 5.0</a><br /><a href="http://www.enthuware.com" target="_blank" rel="nofollow">SCBCD 5.0 mock exam</a> <br /> <br />SCEA 5 Part1: Preparing...
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 687
    
    1
After you create the .bat file, make sure it's on the PATH for your OS to be able to find it.
If you have a bunch of batch files that you want to be able to execute without having to change directory, create a folder for them and put the path to that folder in the PATH variable.
Preetish Madalia
Ranch Hand

Joined: Dec 12, 2007
Posts: 37
Mirko,
Does java -cp ./lib/*.jar MainClass.class will help including all my lib files??
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 687
    
    1
I don't know that wildcards work. Try it and let us know.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3669
    
  15
Originally posted by Norm Radder:
I don't know that wildcards work. Try it and let us know.


You can since Java 6. See the Understanding class path wildcards section in here


Joanne
Preetish Madalia
Ranch Hand

Joined: Dec 12, 2007
Posts: 37
I tried using wildcard but its not working.

I am using the following command.

java -CLASSPATH ./lib/*.jar com.test.MainClass
Its giving ClassNotFoundException
arun nohwar
Greenhorn

Joined: Aug 05, 2008
Posts: 16
Many open source applications like ANT, Tomcat comes with batch file that call java class (to start application). You can look into those batch files, to get more insight into batch file.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3669
    
  15
Originally posted by Preetish Madalia:
I tried using wildcard but its not working.

I am using the following command.

java -CLASSPATH ./lib/*.jar com.test.MainClass
Its giving ClassNotFoundException


Try reading again the document that I linked to for the correct format.
Preetish Madalia
Ranch Hand

Joined: Dec 12, 2007
Posts: 37
Hi All,
The issue is resolved. Wildcard is still not working for me. However, we can use for loop to include jars in classpath, making using of option -R.
Thanks a lot for you replies guys.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3669
    
  15
For anyone who does want to use wildcards, the relevant part of the link is
Class path entries can contain the basename wildcard character *, which is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR.


i.e. it is slightly different to a normal wildcard. * actually means all the JAR files in a directory, not all the files in a directory.
 
 
subject: Running Java program using .bat file.