Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a .java or a .jar file into a Linux executable file ( without a .jar extension, which means it's not a .jar file )

I have been searching for many similar posts about this but I still can't find my answer. I want to convert a .java program into a Linux executable file, without the .jar extension. How can I do it? I am trying to use Launch4j java wrapper, JWrapper, IzPack, making a .sh, making a .bat, running it using java -jar myFile.jar etc. but none of them worked. Some procedures are complicated and difficult to debug. Is there any straightforward way to convert a .java file or .jar file into a Linux executable file?

I need to pass this program as a Linux executable as a whole into another program that takes this program as an argument.

like image 746
hiew1 Avatar asked Apr 03 '15 09:04

hiew1


1 Answers

Let's say that you have a runnable jar named helloworld.jar

Copy the Bash script below to a file named stub.sh

#!/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
java_args=-Xmx1g
exec "$java" $java_args -jar $MYSELF "$@"
exit 1 

Than append the jar file to the saved script and grant the execute permission to the file resulting with the following command:

cat stub.sh helloworld.jar > helloworld.run && chmod +x helloworld.run 

That's all!

Now you can execute the app just typing helloworld.run on your shell terminal.

The script is smart enough to pass any command line parameters to the Java application transparently.

Credits: Paolo Di Tommaso

Source: https://coderwall.com/p/ssuaxa/how-to-make-a-jar-file-linux-executable

like image 163
Dyorgio Avatar answered Oct 05 '22 23:10

Dyorgio