Reputation: 485
I've written a project in eclipse and i would like to run its ".class" file using cmd .
I looked up and i saw i had to use the format :
java -cp . package.mainclass
I tried that and it kinda work, the problem is that my project have a lib directory in it that contains jar files that i use , and when running as above i get the error that classes couldn't be found (other than the main).
How can I run it ?
note : there is a xml .classpath file in the project's directory that contains the names of the jars used .
Upvotes: 1
Views: 733
Reputation: 7692
You need to add all the jars manually listed in .classpath
to your Java
command. Another place to find list of jars
is Project->Properties->BuildPath
You can use custom scripts for generating classpath, for example (*NIX):
java -cp $(echo lib/*.jar | tr ' ' ':') package.MainClass
EDIT:
Ok, so to directly use Eclipse .classpath
file entries to generate classpath in *NIX, you can use following:
java -cp $(echo $(grep ".jar" .classpath | awk -F \" '{print $4}') | tr ' ' ':') package.MainClass
This way you don't have to manually generate classpath. For Windows you would require bash script for doing same, I haven't worked that out yet!
Upvotes: 2
Reputation: 20648
Add the libraries that your application uses to the class path.
java -cp .;lib/my-lib1.jar;lib/my-lib2.jar my.pkg.MainClass
If you also package your classes into a JAR file (which is recommended), you can do the following (assuming the name is main.jar and it is located in the application's root directory):
java -cp main.jar;lib/my-lib1.jar;lib/my-lib2.jar my.pkg.MainClass
But if you already have a JAR file for your application, you will have a manifest file, where you can add the class path to:
Class-Path: lib/my-lib1.jar;lib/my-lib2.jar
In that case, you just start it with
java -cp main.jar my.pkg.MainClass
And - last step - as you already have a main JAR with a manifest file, you can make it a runnable JAR by adding this to your manifest file:
Main-Class: my.pkg.MainClass
Now you can start your application by:
java -jar main.jar
A double-click also works now.
Upvotes: 2
Reputation: 851
use this I hope this would work for you:
java -cp <your jar file location> package.mainclass
Upvotes: 0