Skip to content

Convert Java Executable to Linux Executable

Frédéric Delorme edited this page Oct 23, 2023 · 3 revisions

Introduction

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.

Instruction

This instruction fully tested under Ubuntu 14.04 LTS 64-bit, Java 1.8.0_25 64-bit. If any error has been found, feel free to report to Issues.

  1. 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 stub.sh.

  1. Pack your project into a runnable *.jar program, which is the native Java executable file. Use Eclipse is recommended. (File -> Export... -> Runnable JAR File) Pack the necessary Java Library is also recommended. Export the *.jar file in the same directory of stub.sh.

  2. Run the command below, make sure the current directory is the same one includes stub.sh and the *.jar file.

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

where PROJECT is the name of your *.jar file.

  1. That's all! after this you will have a file named PROJECT.run and you could run it via ./PROJECT.run or sh PROJECT.run. Furthermore, all arguments are supported as the original Java executable file.
Clone this wiki locally