r/java • • Jun 21 '11

Making Really Executable Jars

http://skife.org/java/unix/2011/06/20/really_executable_jars.html
44 Upvotes

12 comments sorted by

3

u/[deleted] Jun 21 '11

I did not know about this. Very neat!

3

u/mikaelstaldal Jun 21 '11

On Linux a jar file executable without any modifications, just use chmod +x.

This works via Binfmt_misc.

2

u/sod1864 Jun 21 '11

Why not just make a self executing jar instead?

2

u/Neebat Jun 21 '11

chomd

That's a chomp that turned wrong at the end, right?

2

u/skeeto Jun 21 '11 edited Jun 21 '11

You can skip the shell part, though this makes assumptions about the location of java.

$ cat prepend
#!/usr/bin/java -jar

$ cat prepend example.jar > example
$ chmod +x example
$ ./example
Hello, World

My system lets me run jar files directly anyway. I don't know what part of my system is allowing this to work correctly, though.

$ chmod +x example.jar
$ ./example.jar
Hello, World

1

u/bjarneh Jun 21 '11

that does not work...

a shell-script stub with a jar-file contained inside the script does work however, makeself uses that apporoach. but it needs to pipe the exact jar-file (bytes of the shell-script) into a file (tmp_whatever.jar for instance) and then run java -jar tmp_whatever.jar $@ on that file.

0

u/[deleted] Jun 21 '11

[deleted]

1

u/bjarneh Jun 21 '11

i stand corrected :-)

1

u/64-17-5 Jun 21 '11 edited Jun 21 '11

I have no clue what he is talking about. But I got a similar problem related to *.jar's. I have this software that I am developing, and some users have complained they can't execute the *.jar file by double clicking the file, even though it works perfectly well on windows computers. A user suggested making a script, like this post. I guess there will be two executables then, one for the specific operative system that got a problem with *.jars and one for windows computers. Isn't there a easier way of doing this that involves only one file?

Edit: And btw I'm a total noob. I have no clue how to make such a script file anyway so...

2

u/Mchr3k Jun 21 '11

Jar files are only runnable on windows thanks to a file association setup when the JVM is installed. If a user installs another program which handles jars this will break the ability to double click a jar to run it. For example I used to have a Nokia phone and the windows software for it registered to handle jars so that it could try and install them on my phone.

1

u/64-17-5 Jun 21 '11

Oh crap. I want a user-friendly program. What should I do?

3

u/Mchr3k Jun 21 '11

Eclipse is the best example of a cross platform java program used by lots of people. I would take a look at what they do. I suspect you will need a per platform launcher script/program. There are various java packaging tools which can help you with this but I don't have any specific suggestions.

0

u/[deleted] Jun 21 '11

Nowadays I use Groovy for my command line needs. It can even pull dependencies automatically from a Maven repository using the @Grab annotation.

No need to do a java -jar ./waffles.1.2.3.jar -foo=bar

A simple groovy Foo (or, if you shebang the script and make it executable it is even simpler, ./Foo.groovy).