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.
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).
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.
In the other disciplines, we rise by standing on each others' shoulders. In computer science, we do it by standing on each others' toes.
Joined: Jul 16, 2012
Thanks for the replies.
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?
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.
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.
Joined: Jul 16, 2012
Thanks for the reply guys.
To resume, when you have a console application it would be best to run it via a script.