Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java -jar XXX does nothing

 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys : I exported a Jar from eclipse, and then, by issuing the command

java -jar package.MyMainClass

I expected to see some output, however, nothing happened.

What does this mean ? Does this mean that

1) The jar command is failing due to bad syntax --- I assume this is NOT the case ?

or

2) The jar command syntax valid, but for some reason, it does not call the main method in my class.

Thanks for any feedback.

PS I have tried exporting the jar with and without a defined executable run class...

here is the jar creation script description below

 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The strange thing is, however, that I CAN integrate this jar into an application and use it, perfectly.... i just cant invoke its services via that "java -jar xxx" method..... Ive never noticed anything like this.... I wonder if there an "Executable" attribute to jars or something, that can be turned on or off via some type of configuration ???
 
Greg Charles
Sheriff
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you were right with your first guess, though you assumed you weren't. Correct syntax is:

java -jar venn3_core.jar

which will run jmolwrap.JayMolScript (assuming there is a main method there)

or

java -cp venn3_core.jar:$CLASSPATH package.MyMainClass

 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry I tried java -jar xxx.jar ClassName and tha failed. My first post was a typo.....
The command has no effect. Very strange .!!!
 
Greg Charles
Sheriff
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, again, java -jar jarfile ClassName isn't correct syntax either, but it's closer. That should run the main class defined in your manifest file and pass it ClassName as an argument ... an argument it most likely won't understand though. If you think that command should be running the ClassName class, you're probably going to be disappointed.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jay vas wrote:I'm sorry I tried java -jar xxx.jar ClassName and tha failed. My first post was a typo.....
The command has no effect. Very strange .!!!


Any class name specified on the command line when using the -jar option is ignored. As Greg said, when you use the -jar option the main method that will be run is the one in the class specified in the Main-Class entry in your jar's manifest file which, in your case, is jmolwrap.JayMolScript. If this is wrong then you need to change your manifest file.
 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats strange that you cannot run a main class of your choosing when invoking via the java -jar command .... is there any reason why this is the case ? It seems only natural that invocations of a "java -jar" command might require, in certain circumstances, the use of a different main class . At the very least, I wouldnt be surprised if people have requested this feature in the past ?
 
Henry Wong
author
Marshal
Pie
Posts: 21362
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jay vas wrote:Thats strange that you cannot run a main class of your choosing when invoking via the java -jar command .... is there any reason why this is the case ? It seems only natural that invocations of a "java -jar" command might require, in certain circumstances, the use of a different main class . At the very least, I wouldnt be surprised if people have requested this feature in the past ?


That is how it is defined... When you use the -jar option, it gets the main class from the manifest. If you don't want to use the class specified in the manifest, then use the -cp option, and specify the main class.

Henry
 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay.... well then for clarity, I guess there is an EXCEPTION to Joann's comment 'when you use jars, any Class Name is ignored', which is that the "cp" flag causes the java -jar command to pay attention to a class name you specify at the end, and use that class as the main class, regardless of what is in the manifest.

For beginners : Java -jar -cp com.mypackage.hello.HelloWorld will run HellWorld.

Thanks guys for all your help my issue is resolved........


 
Henry Wong
author
Marshal
Pie
Posts: 21362
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jay vas wrote:Okay.... well then for clarity, I guess there is an EXCEPTION to Joann's comment 'when you use jars, any Class Name is ignored', which is that the "cp" flag causes the java -jar command to pay attention to a class name you specify at the end, and use that class as the main class, regardless of what is in the manifest.


It is not an EXCEPTION, you are not supposed to mix the -jar and -cp options. If you want to specify a class that is in a jar file, then you use -cp to specify the jar file, and you also need to specify the class.\

Henry
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic