Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a .jar to an .exe?

Tags:

java

windows

exe

I want to convert a .jar to an .exe for microsoft. Is there any program converter for this?

Also if there's one for Mac and Linux I would appreciate suggestions for those too.

like image 688
Negrodamus12 Avatar asked Dec 02 '10 01:12

Negrodamus12


People also ask

Can you turn a Java file into an exe?

I would say launch4j is the best tool for converting a java source code(. java) to .exe file You can even bundle a jre with it for distribution and the exe can even be iconified.

Is .jar and .exe same?

An Exe file is an executable file that can be executed in Microsoft OS environment. Jar file is container of Java Class files, including other resources related to the project. Jar file can be executed only if Java run time environment.

What is the difference between jar and executable jar?

FYI: In simple terms, the difference between a JAR file and a Runnable JAR is that while a JAR file is a Java application which requires a command line to run, a runnable JAR file can be directly executed by double clicking it.


1 Answers

Launch4j works on both Windows and Linux/Mac. But if you're running Linux/Mac, there is a way to embed your jar into a shell script that performs the autolaunch for you, so you have only one runnable file:

exestub.sh:

#!/bin/sh MYSELF=`which "$0" 2>/dev/null` [ $? -gt  0 -a -f "$0" ] && MYSELF="./$0" JAVA_OPT="" PROG_OPT="" # Parse options to determine which ones are for Java and which ones are for the Program while [ $# -gt 0 ] ; do     case $1 in         -Xm*) JAVA_OPT="$JAVA_OPT $1" ;;         -D*)  JAVA_OPT="$JAVA_OPT $1" ;;         *)    PROG_OPT="$PROG_OPT $1" ;;     esac     shift done exec java $JAVA_OPT -jar $MYSELF $PROG_OPT 

Then you create your runnable file from your jar:

$ cat exestub.sh myrunnablejar.jar > myrunnable $ chmod +x myrunnable 

It works the same way launch4j works: because a jar has a zip format, which header is located at the end of the file. You can have any header you want (either binary executable or, like here, shell script) and run java -jar <myexe>, as <myexe> is a valid zip/jar file.

like image 152
Matthieu Avatar answered Oct 09 '22 09:10

Matthieu