3

I have written a program in Java that one can use via the Terminal CLI. I've packaged it in a .jar, and it works nicely, however it happens to be a tool that I'd like to use quite often via CLI.

I am familiar with executing .jar files using:

java -jar MyJarFile.jar 

but I'd like to be able to run this using a single command.

The only way I've done something similar to this is by using an NSTask object in Obj-C (my skills in C/C++ are limited), but this is a slightly different situation.

The solution might be staring me in the face, and might be quite simple, but any help to find a different method for launching my .jar would be greatly appreciated.

Thanks

2
  • 1
    You could write a shell script or place a "short cut" into the ".profile" or ".bash" or what ever that file is :P - See the section Making Your Own Shorthand Commented Aug 11, 2015 at 1:04
  • The easiest way would be to simply use an alias. Commented Aug 11, 2015 at 1:18

4 Answers 4

1

Shell script would work too.

#!/bin/bash java -jar MyJarFile.jar 

save this as RunMyJarFile.sh and give it execute permissions.

./RunMyJarFile.sh 

is all you need to do.

Sign up to request clarification or add additional context in comments.

1 Comment

I just ended up using a shell script. It'll work fine. Thank you.
0

Just add an alias to your .bashrc like this:

echo "alias mycmd=\"java -jar MyJarFile.jar\"" >> ~/.bashrc 

then source it (or reopen the terminal) with:

source ~/.bashrc 

now you can just type mycmd.

Comments

0

you can use Launch4J or other java wrappers.

Comments

0

You've got a couple of choices:

  • Just click on the Jar from the GUI - on most (all?) platforms, double-clicking on executable Jars will run them. If that works for your use case, that's the easiest thing to do.
  • Just put your java -jar ... call into a shell script; put it on your PATH if you want. Quick and easy, but it requires keeping track of both the shell script and the Jar file, which might end up being a case of "now you have two problems".
  • Use a wrapper utility to convert your Jar into a binary or .exe installer. These will bundle your Jar with a script or binary that will unpack and run your Jar automatically for you. It's been a couple years, but I've used NSIS to create an .exe installer pretty painlessly.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.