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.

share|improve this question
1  
why you wanna make .sh file for linux only? i guess the beauty of java is cross platform you just need to install JRE and one single jar will run perfectly OK on any OS. – Muhammad Suleman Apr 3 '15 at 10:01
    
I have tried making a .sh file but it doesn't work. The reason is because I have a main program that is fixed and written by someone else and I can only pass in my program as a Linux executable (not a .jar) to it. And I am developing it using Java. – hiew1 Apr 3 '15 at 10:03
1  
@pythonhiew : i guess you can also run jar using command like, java -jar fileName.jar. why don't you make a bash which run command and up your JAR. – Muhammad Suleman Apr 3 '15 at 10:14
1  
I don't see why .sh script would be any different than "linux executable", it's executable as well. Large portion of linux executables are actually scripts - if that doesn't work then I don't see how any other executable would work either. Also: Launch4j is for creating native windows executables, I don't see how that would help you in Linux environment. – eis Apr 3 '15 at 11:26
1  
Make sure your .sh file is really executable (chmod +x yourfile.sh), maybe that's the problem. If the tool you are passing the shell script to doesn't like the .sh ending, just rename it omitting the .sh ending. – muued Apr 3 '15 at 11:36
up vote 3 down vote accepted

I found a solution, which is exactly what I want after hours of searching and trying. It is to use the gcj command in linux.

It works like gcc for C and g++ for C++ which compile C or C++ programs into executables.

First install gcj by using the terminal by typing:

sudo apt-get install gcj-jdk

After that, cd into the directory of where the .jar file is located, let's say the .jar file is myFile.jar, then do:

gcj myFile.jar -o newNameofTheFile --main=theMainClassNameofTheFile

to compile the .jar file. And it should work like an executable by just running it in the command line like this:

./newNameofTheFile
share|improve this answer
4  
gcj has been considered obsolete for years. – chrylis Apr 3 '15 at 13:26
1  
Yeah chrylis however it works after all the methods I tried. Like running it with java -jar myFile.jar etc. or using .bat, .sh, all failed. Yet this works successfully. Because I have to pass this compiled program into another program as an argument and the other program does not take any form of argument, namely .jar, .sh, .bat, except after what is compiled by gcj. – hiew1 Apr 3 '15 at 13:30
    
@pythonhiew nice that it worked, but it will not work if you're using modern Java constructs, and doing this is really no good idea if you're interested the least in performance. It's an emergency solution, nothing more. – Marcus Müller Jul 2 '15 at 12:35
    
I haven't tried it myself but for anyone interested, this should interest you (tl;dr: it really is obsolete). – MasterMastic Feb 22 '16 at 14:18

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 > hello.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

share|improve this answer
    
What is $java_args ? – Thomas Decaux May 3 '17 at 8:05
    
It could be any JVM arg, like -Xmx, -Xms, etc... Example updated. – Dyorgio May 3 '17 at 14:25

What you can do is make a tiny little C-program, that calls the one of the exec functions (see man 3 exec) to the "java" binary, passing "-jar", "xxx.jar" as arguments, see also Forking a new process in C++ and executing a .jar file

share|improve this answer
    
Bram thanks for your answer. I have found an easy solution and I have posted the answer below. – hiew1 Apr 3 '15 at 13:20
    
I upvoted your answer anyway because it makes sense. I haven't tried it but it's quite a good idea. – hiew1 Apr 3 '15 at 13:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.