Requirements for the Executable .jar
- Class files
- Something with a main method to run. I'll be starting with the quintessential 'Hello World' project.
- Manifest file
- The manifest tells the jvm what you want the point of entry to be for the jar.
Hello World!
My little java class lives in the demo package, and prints "Hello World!" and then lists the arguments passed in.
package demo;
public class HelloWorld {
public static void main (String[] args) {
System.out.println("Hello World!");
for (String each : args)
System.out.println (each);
}
}
Compile the classfile (commandLine$ javac HelloWorld.java) and you're good to go.
Manifest.txt
This file is just one line, and ends with a newline
Main-Class: demo.HelloWorld
Putting it all together
Here's what I have so far~/MANIFEST.txtThe .java file isn't really necessary from this point forward, but if you want to go back and look inside your jar for the source code you'll want to keep it around.
~/demo/
~/demo/HelloWorld.java
~/demo/HelloWorld.class
Create the .jar
Use the jar utility to package up the jar. cmfv = create, manifest (filename), filename (for jar), verbose
jar cmfv MANIFEST.txt demo.jar demo
added manifest
adding: demo/
adding: demo/HelloWorld.java
adding: demo/MANIFEST.MF
adding: demo/HelloWorld.class
The Result
All said in done, we're left with demo.jar
When you run java -jar demo.jar in the command-line, you get the result below
steve:~/Desktop$ java -jar demo.jar
Hello World!
You can also add extra parameters and they will be printed below
steve:~/Desktop$ java -jar demo.jar what\'s\ up?
Hello World!
what's up?
No comments:
Post a Comment