Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I able to execute JARs directly from bash?

Tags:

java

linux

bash

I'm a longtime Java guy, and know that the way to run a JAR with a main class referenced in a MANIFEST.MF file within the Jar is easy:

java -jar theJar.jar

I was using this to start up the Fabric3 server (contained in bin/server.jar in its standard distribution). I noticed that when I unpacked it from the distribution tarball, it was flagged as executable. On a whim, I tried

./server.jar

from my bash command line (bash version 4.1.5 in Ubuntu 10.10), and lo and behold, the server started up as if I had typed the normal java -jar ... command. The JAR is structured like a normal JAR; I did a head on it, and there was no #! command in the first few bytes, so bash shouldn't magically know to start a Java VM with it, right? Or has this version of bash grown the ability to start JARs with a proper manifest correctly? Inquiring minds want to know...

like image 212
aarestad Avatar asked Nov 11 '10 22:11

aarestad


1 Answers

This may not be a feature of bash at all. If you are running linux (as you have tagged) you may have the binfmt_misc kernel module, which does a little bit of magic, when you try to execute a program. Basically what this does is it extends the binary formats that linux can execute from just elf to also include some user defined formats, with enough instructions to run them.

like image 73
SingleNegationElimination Avatar answered Oct 07 '22 23:10

SingleNegationElimination