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?
2 Answers
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
-
1+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.– PSpeedCommented Nov 21, 2009 at 4:13
-
Shouldn't (or couldn't) you use backslashes in the classpath in the Windows example? Commented Nov 22, 2009 at 1:59
-
Java will happily use forward slashes under Windows so it is sometimes easier/cleaner to just do it that way on all platforms.– PSpeedCommented Nov 22, 2009 at 5:38
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.