Two Laptop Bag*
The moose likes Beginning Java and the fly likes issue with jar creation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "issue with jar creation" Watch "issue with jar creation" New topic
Author

issue with jar creation

Kris Ram
Greenhorn

Joined: Jul 16, 2012
Posts: 10
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Stevens Miller
Ranch Hand

Joined: Jul 26, 2012
Posts: 523
    
    3

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

Joined: Jul 16, 2012
Posts: 10
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
Ranch Hand

Joined: Jul 26, 2012
Posts: 523
    
    3

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

Joined: Oct 13, 2005
Posts: 37950
    
  22
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

Joined: Jul 16, 2012
Posts: 10
Thanks for the reply guys.

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


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: issue with jar creation
 
Similar Threads
Runnable jar is not including the files
Modifying JBOSS twiddle code
insertin g a class in an applet
Classpath troubles
Decompile class files of Java 1.5