Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

issue with jar creation

 
Kris Ram
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I have created a very simple project, no IDE used, and it takes input from System.in using Scanner and prints the output with System.out. I wanted to understand the jar tool.

The are no packages and the main class is found directly the project folder, and the path is c:Training\java\jartool\CalcPrj.

I have navigated to the above folder on the command line and typed the following command


I tried running the project by double clicking on it (the Java Icon appears on the file btw), but nothing happens. I then used the Java Decompiler to check inside the jar and saw that it is using the path as a package.

Although when using the jar tf command, there does not seem to be any package.

Any idea why the decompiler shows the package as such.


Regards
Kris
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you just package the class file like that, the resulting JAR file will not be an executable JAR file, so that you can't run the program by double-clicking on it. To make it an executable JAR file, you need to include a manifest file in it, which specifies what the class is to run (the class that contains the main() method).

See Lesson: Packaging Programs in JAR Files to learn how to do this.
 
Stevens Miller
Bartender
Posts: 1223
23
C++ Java Netbeans IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try adding the name of the class with your main routine in it when you create the .jar file, like this:

(That's assuming your class with the main routine is called "CalcPrj." Change it to whatever it actually is, if that's not it.)

This specifies the entry point. Probably, by double-clicking on a .jar with no entry point specified, a command window (are you in Windows?) comes and goes for the lifetime of the run of java.exe, only to show an error message, and it happens so fast you don't see it. In your command window, try this to run your .jar file:


Also, I'd avoid using just "*" as an argument to jar; among other things, you'll end up with your .java files in your .jar file, which is not what you want, typically.
 
Kris Ram
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies.

Stevens,
the package issue is no longer occurring because I was running the jar command from one folder level up. This is sorted.

But the program is still not executing.

Previously, the Manifest did not contain the main class, but when I used your command it now has the class.


When I run the class after compilation, it waits until the user inputs something. It also works by launching from the command line I you said below (java -jar). This should be the same when double clicking the jar, right?




Regards
Kris
 
Stevens Miller
Bartender
Posts: 1223
23
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like what I said before was just wrong, Kris. When you double-click that .jar file, you don't get a command window. If your program used a GUI instead of the console, it would work the way you want it to, by double-clicking.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48964
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You usually find .jar files are opened by the javaw tool rather than the java tool. Javaw does not usually create a terminal window or a console.
Corollary: using System.console() or whatever will return null, and any attempt to use the console will cause a null pointer exception.
 
Kris Ram
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply guys.

To resume, when you have a console application it would be best to run it via a script.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic