3

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
  • Well? Did my answer work out for you? Please provide an answer if you need an other solution.
    – jitter
    Commented Dec 5, 2009 at 15:44
  • I used intellij mvn exec runner. This worked out fine. All others did not work in my situation. Commented Dec 8, 2009 at 2:16

2 Answers 2

12

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
3
  • 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.
    – PSpeed
    Commented 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.
    – PSpeed
    Commented Nov 22, 2009 at 5:38
0

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.