28

Is it possible to run a Java app which doesn't contain MANIFEST.MF file? Of course, there's static main method,just lacks manifest file. And the app is depending on several external .jar files.

If is this possible, how to do that?

eonil
  • 83,476
  • 81
  • 317
  • 516

4 Answers4

30

It is possible, you can specify the class to run from the command line:

java -cp yourJar.jar your.main.Class

Same question here:

How to run a class from Jar which is not the Main-Class in its Manifest file

Community
  • 1
  • 1
BackSlash
  • 21,927
  • 22
  • 96
  • 136
  • 1
    It's strange that the above works but `java -jar your.main.Class` does not. – Sridhar Sarnobat Mar 17 '18 at 02:22
  • @backslash, by running above code if getting Error like : "Could not find or load main class" What to debug ? – Ishita Shah May 29 '18 at 09:09
  • @IshitaShah It means that the class could not be found or is not a main class. Did you check the class name and the package name? Are you 100% sure your class has a correct main method definition? – BackSlash May 29 '18 at 12:30
  • My class does not has main method, But I have include one of the plugin and after that I execute and got this error. – Ishita Shah May 29 '18 at 12:32
  • 1
    @IshitaShah The class you run ***must*** have a main method. It's the first method run by the JVM, if you don't have that your app won't run. That's why you get that error. – BackSlash May 29 '18 at 12:38
  • @BackSlash, That is the Problem only. I am having project with TestNG, where java main method is override by Test annotation. So there is no main method. – Ishita Shah May 29 '18 at 12:51
  • 1
    @IshitaShah TestNG has a completely different way of running tests, you need to go through their documentation to learn how to run tests from command line. I'm not familiar with TestNG, so I can't help. – BackSlash May 29 '18 at 13:20
12

You can also add the manifest, with the following command:

jar  -uvfe  your.jar foo.bar.Baz
java -jar your.jar        # tries to run main in foo.bar.Baz
Ingo
  • 36,037
  • 5
  • 53
  • 100
3

Of course! Just use this:

java -cp MyJar.jar com.example.Main
madhead
  • 31,729
  • 16
  • 153
  • 201
3

Yes , use this

java -cp myJar.jar package.className

user3886907
  • 345
  • 5
  • 7