Java program is universal and applicable to most operating systems, but if someone intends to create a native executable binary file for certain operation system, is that possible?
The answer is yes. Here is the instruction to create an executable program for Linux with .run
as its extension name.
Create a text file use your favorite text editor (In this case it is gedit) and type these code below:
#!/bin/sh
MYSELF=`which "$0" 2>/dev/null`
[ $? -gt 0 -a -f "$0" ] && MYSELF="./$0"
java=java
if test -n "$JAVA_HOME"; then
java="$JAVA_HOME/bin/java"
fi
exec "$java" $java_args -jar $MYSELF "$@"
exit 1
save as runner.sh
Pack your project into a runnable *.jar
and export the *.jar
file in the same directory of runner.sh
.
$ java -jar PROJECT.jar
There are plenty of tutorials showing how to implement this feature using Ant, Maven, Eclipse, Netbens, etc.
Anyway in its basic form, it just requires to add a manifest.mf
file to the jar package. The manifest must contain an entry Main-Class
that specifies which is the class defining the main method for your application. For example:
$ javac HelloWorld.java
$ echo Main-Class: HelloWorld > manifest.mf
$ jar -cvmf manifest.mf helloworld.jar HelloWorld.class
Run the command below, make sure the current directory is the same one includes runner.sh
and the *.jar
file.
cat runer.sh PROJECT.jar > PROJECT.run && chmod +x PROJECT.run
where PROJECT is the name of your *.jar
file.
That's all! after this you will have a file named PROJECT.run
and you could run it via:
./PROJECT.run
or just
sh PROJECT.run
Furthermore, all arguments are supported as the original Java executable file.