2

I have a jar file with a main class in it. The main class is already configured in the manifest file, meaning the jar can be executed in the following way:

java -jar Main.jar

I'd like to make it executable, meaning running it in the following way, without the need to provide any parameter. Windows:

Main.cmd

or Linux:

Main.sh

How can I package the jar into an executable?

7
  • There's an answer for Linux. Any idea how to package a java jar in an executable for Windows? Commented May 10, 2015 at 11:54
  • Honestly, how hard is it to search the net for "jar to exe"? There are a number of solutions available.
    – Karan
    Commented May 11, 2015 at 20:30
  • @karan great, then it will be easy to answer the question. Commented May 12, 2015 at 3:46
  • Definitely. You can edit your existing answer and add it in, then self-accept.
    – Karan
    Commented May 12, 2015 at 4:26
  • 1
    It's ok, you're welcome to it. :) In all seriousness though, a single canonical answer would be better because you've asked about multiple OSes, and unless an answer covers both it wouldn't be complete.
    – Karan
    Commented May 12, 2015 at 5:21

1 Answer 1

3

For Linux, there's a way: Package the jar in a bash file. Steps:

1- Create a stub:

MYSELF=`which "$0" 2>/dev/null`
[ $? -gt 0 -a -f "$0" ] && MYSELF="./$0"
exec java -jar $MYSELF "$@"
exit $?

2- Concatenate the stub and the jar into a new executable:

cat stub.sh Main.jar > main.sh

3- Make the new file executable:

chmod +x main.sh

That's it! Source: https://coderwall.com/p/ssuaxa/how-to-make-a-jar-file-linux-executable

1
  • You can use shell emulators to run the shell script on Windows as well. Example of such emulator: GitBash and Cygwin. Commented May 12, 2015 at 5:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .