nintendo switch

Make .jar program "executable"

Recently, I wrote a command-line version of Little Bull Translator in java, and generated a .jar file using maven. However, as we all know, .jar programs cannot be directly executed, but require the use of the JVM:

java -jar filename.jar ...arguments

This makes the user experience less friendly. So, is there a way to make the .jar program "executable" (without having to use java -jar)? I looked it up and found a solution.

The content is mainly referenced from this article.

There are two types of executable programs on Linux: binary programs and script files. The former is machine code and can be directly executed, while the latter requires an interpreter to interpret and execute the code. In a script file, you can specify the interpreter by adding a hashbang at the beginning of the file. When executing the script file, the system will use the interpreter specified in the hashbang to interpret the script content.

Taking python as an example: normally, we need to execute a program by using python, but if we add #!/usr/bin/python at the beginning of and give it executable permission, it can be executed directly as ./

Now, back to java, the .jar file itself is a collection of bytecode, and java -jar indicates that it needs to be interpreted by the JVM. Therefore, our approach is similar to python:

  1. Create a new file and write the hashbang at the beginning.

    touch a
    echo "#!/usr/bin/java -jar" > a
  2. Append the content to be interpreted (the entire .jar file) to the file.

    cat /path/to/.jar >> a
  3. Give the file executable permission.

    chmod +x ./a

Afterwards, it can be used as a regular executable file!

Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.