views:

103

answers:

2

I have a jar file with several classes which have static main methods. Can I execute them inside the jar from the command line? If not, can I execute them one by one?

+9  A: 

Windows

java -classpath .;path/to/yourlib.jar your.package.path.ClassWithMain

Linux (I guess)

java -classpath .:path/to/yourlib.jar your.package.path.ClassWithMain

Or if you don't use packages just do (for Windows)

java -classpath .;path/to/yourlib.jar ClassWithMain
jitter
+1 but java -cp path/to/yourlib.jar your.package.path.ClassWithMain should work too. The . is superfluous if the only classes are in the jar.
PSpeed
Shouldn't (or couldn't) you use backslashes in the classpath in the Windows example?
Derek Mahar
Java will happily use forward slashes under Windows so it is sometimes easier/cleaner to just do it that way on all platforms.
PSpeed
A: 

If you don't know which class has static main method,you can use some java IDE,for example IntelliJ IDEA,it can find the classes with main() method,and then you can run it within your project.

liya