I spent some time figuring out how to package some files in a jar file, and make it runnable. Apparently, it makes a difference if your project has a packagename. This guide assumes so.
Create a folder named testapp somewhere.
cd /tmp
mkdir testapp
cd testapp
The package name for our app will be
no.dvikan.testapp
Now, create a file structure which mirrors the package name.
mkdir -p src/no/dvikan/testapp
Inside the src/no/dvikan/testapp folder, create a file named Main.java and put a main method inside there:
package no.dvikan.testapp;
public class Main {
public static void main(String[] args) {
System.out.println("Goodbye cruel world");
}
}
Let us now compile the program. First create a folder named bin under testapp.
mkdir bin
Then compile:
javac -d bin/ src/no/dvikan/testapp/Main.java
Very soon, you are ready to create the runnable jar file. But first, you must create a manifest file which says which file contains the main() method.
Here is manifest.txt. Save it in the root folder. That is, in the testapp folder.
Main-Class: no.dvikan.testapp.Main
It is very important that there is a newline at the end of the manifest.txt file.
Now create the runnable jar:
jar cvmf manifest.txt hello.jar -C bin/ .
Run the jar:
java -jar hello.jar
Success!