1

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?

  • There's an answer for Linux. Any idea how to package a java jar in an executable for Windows? – AlikElzin-kilaka May 10 '15 at 11:54
  • Honestly, how hard is it to search the net for "jar to exe"? There are a number of solutions available. – Karan May 11 '15 at 20:30
  • @karan great, then it will be easy to answer the question. – AlikElzin-kilaka May 12 '15 at 3:46
  • Definitely. You can edit your existing answer and add it in, then self-accept. – Karan May 12 '15 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 May 12 '15 at 5:21
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

  • You can use shell emulators to run the shell script on Windows as well. Example of such emulator: GitBash and Cygwin. – AlikElzin-kilaka May 12 '15 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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