0

In console running a jar file is normally done by java -jar application.jar Is there a way to run jar files by executing ./application.jar? I'd like this behaviour for any jar file. I tried chmod +x application.jar but it won't work as it gives the following error: invalid file (bad magic number): Exec format error. Further, it would be specific to one application.

menteith
  • 271
  • Maybe makefile might help here! – George Udosen Dec 17 '17 at 00:04
  • @GeorgeUdosen Could you be more specific, please? – menteith Dec 17 '17 at 00:15
  • See my answer menteith! – George Udosen Dec 17 '17 at 00:52
  • Use binfmt-misc, but with ':ExecutableJAR:E::jar::/usr/local/bin/jarwrapper:' instead of :CLR:M::MZ::/usr/bin/mono:, where jarwrapper essentially runs java -jar "$@". See https://wiki.archlinux.org/index.php/Binfmt_misc_for_Java for more – muru Dec 17 '17 at 01:10
  • Or simpler: https://askubuntu.com/a/978003/158442 – muru Dec 17 '17 at 01:22
  • @muru Thanks for this. I installed jarwrapper but when I enter ./myjarfile.jar zsh shows this error: zsh: permission denied: ./myjarfile.jar. After that I created /etc/binfmt.d/ExecutableJAR.conf with the following content :ExecutableJAR:E::jar::/usr/bin/jarwrapper:. I changed the path to jarwrapper because which jarwrapper shows /usr/bin/jarwrapper. Then I reboot my system. After that I'm still getting zsh: permission denied: ./myjarfile.jar. What's wrong? – menteith Dec 17 '17 at 12:59
  • @menteith is the file executable? You still need it to be executable – muru Dec 17 '17 at 13:10
  • It works after setting the file executable. Many thanks! – menteith Dec 17 '17 at 15:08

1 Answers1

0

.jar file is not executable by itself.. its just a modified(compiled and ziped) text file; the executable is the /usr/bin/java which sits on a path known to the system; that is why you can type java -jar app.jar and it works

./command means this folder in another shell loaded in this one(the command shell);

. command means this folder this shell

http://www.linfo.org/dot_slash.html

you can define an alias for the repetitive part java -jar

temporary

type alias inside a terminal to see already defined aliases

type your presumed alias to see if it returns command not found so you can use it

then type alias command='java -jar' to define the alias

after that you can use it as command app.jar

permanent

edit the .bashrc file from your home folder and add an alias like the one in temporary save and exit and relog

http://www.linfo.org/alias.html