Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
One-liner to turn jar with Main-Class into executable shell script
# turn a jar with a Main-Class into a stand alone executable
(echo '#!/usr/bin/env java -jar'; cat blahblah.jar) > blah
# turn a jar with a particular main clas into a stand alone executable
(echo '#!/usr/bin/env java -jar package.MainClass'; cat blahblah.jar) > blah
@spullara

This comment has been minimized.

Copy link

spullara commented Jan 20, 2011

I'm pretty sure that the "$@" doesn't work for quoted strings on the command line. I ran into issues and had to come up with that insanity to get past them. Do you think it does?

@briandealwis

This comment has been minimized.

Copy link
Owner Author

briandealwis commented Jan 20, 2011

The insanity came as you were using "eval" -- that causes the shell to expand and interpret the arguments provided. My modified snippet uses "exec": more efficient (since it causes the running shell to replace itself), and it doesn't do any interpretation of the arguments.

@spullara

This comment has been minimized.

Copy link

spullara commented Jan 20, 2011

Ah, thanks for the insight!

@stuckj

This comment has been minimized.

Copy link

stuckj commented May 13, 2013

This unfortunately doesn't work in Linux though it works great on my Mac. Linux interprets everything after the interpreter in the shebang as one big argument. So, env ends up getting "java -jar" as the exe.

While not as portable, this slight modification works. Does require java to be in /usr/bin though. :-P

(echo `#!/usr/bin/java -jar'; car blahblah.jar) > blah

@mhewedy

This comment has been minimized.

Copy link

mhewedy commented Apr 26, 2014

@stuckj

Thanks for your help regarding Linux..
Beware of typo.. the command should look like

(echo '#!/usr/bin/java -jar'; cat blahblah.jar) > blah

Thanks.

@briandealwis

This comment has been minimized.

Copy link
Owner Author

briandealwis commented Aug 17, 2016

Doesn't seem to work any more 😞 Which is strange as jar tf file shows the jar contents.

@rjmunro

This comment has been minimized.

Copy link

rjmunro commented Sep 4, 2019

This worked for me, both in mac os and in a linux docker image.

@briandealwis

This comment has been minimized.

Copy link
Owner Author

briandealwis commented Sep 4, 2019

It's working for me too. I really should have included some detail such as the OS and JVM versions that I encountered the problem.

/me blushes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.