1

I often run Java .jar files, but in order to do so, I must invoke it with the command java -jar myJar.jar. Simply making the .jar executable will not allow it to be run, since it is not fully compiled. The .jar must also be in my current directory to do so.

What I am asking is is there any way to make it so that I can run a .jar by simply invoking it with myJar, provided it is in somewhere defined by $PATH? Is there any way to specify that it must be run with the Java interpreter?

I am using the Oracle JDK.

theJack
  • 131
  • 1
    You could make an alias or function that does java -jar part. I have done something similar with file manager some time ago to double click on jar file and run them in open terminal. But i haven't seen simply calling a jar file by name. Good question. – Sergiy Kolodyazhnyy Jul 25 '16 at 01:09
  • 7
    Possibly related to http://askubuntu.com/questions/101746/how-can-i-execute-a-jar-file-from-the-terminal (particularly the second answer) – Nick Weinberg Jul 25 '16 at 01:11
  • @NickWeinberg That is basically exactly what I was looking for. Please post it as an answer so that I can accept it. – theJack Jul 26 '16 at 00:08

1 Answers1

-1
echo '#!/usr/bin/java -jar' > my-command

cat myJar.jar >> my-command

chmod +x my-command

./my-command

This answers your question (for a particular jar). If you make your own jars, it's easy to do the above using exec-maven plugin. If it's not your own jars, you could write a bash script to automate the above for multiple files.