• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Running Java program using .bat file.

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java -cp <all your jars, including the one which has the main class> <main class>
 
Rancher
Posts: 5014
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mirko,
Does java -cp ./lib/*.jar MainClass.class will help including all my lib files??
 
Norm Radder
Rancher
Posts: 5014
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know that wildcards work. Try it and let us know.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Preetish Madalia
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried using wildcard but its not working.

I am using the following command.

java -CLASSPATH ./lib/*.jar com.test.MainClass
Its giving ClassNotFoundException
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
What's that smell? Hey, sniff this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic