I have installed an application, when I try to run it (it\'s an executable jar) nothing happens. When I run it from the commandline with:
java -jar \
I had the same problem. A lot of the solutions mentioned here didn't give me the whole picture, so I'll try to give you a summary of how to pack jar files from the command line.
If you want to have your .class
files in packages, add the package in the beginning of the .java
.
Test.java
package testpackage;
public class Test
{
...
}
To compile your code with your .class
files ending up with the structure given by the package name use:
javac -d . Test.java
The -d .
makes the compiler create the directory structure you want.
When packaging the .jar
file, you need to instruct the jar routine on how to pack it. Here we use the option set cvfeP
. This is to keep the package structure (option P
), specify the entry point so that the manifest file contains meaningful information (option e
). Option f
lets you specify the file name, option c
creates an archive and option v
sets the output to verbose. The important things to note here are P
and e
.
Then comes the name of the jar we want test.jar
.
Then comes the entry point .
And then comes -C .
to get the class files from that folder, preserving the folder structure.
jar cvfeP test.jar testpackage.Test -C . testpackage/
Check your .jar
file in a zip program. It should have the following structure
test.jar
META-INF
| MANIFEST.MF
testpackage
| Test.class
The MANIFEST.MF should contain the following
Manifest-Version: 1.0
Created-By: (Oracle Corporation)
Main-Class: testpackage.Test
If you edit your manifest by hand be sure to keep the newline at the end otherwise java doesn't recognize it.
Execute your .jar
file with
java -jar test.jar