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

dimo414
  • 47,227
  • 18
  • 148
  • 244
yurisend
  • 123
  • 1
  • 6
  • 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](http://lifehacker.com/5743814/become-a-command-line-ninja-with-these-time-saving-shortcuts) – MadProgrammer Aug 11 '15 at 01:04
  • The easiest way would be to simply use an alias. – too honest for this site Aug 11 '15 at 01:18

4 Answers4

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.

Sid
  • 465
  • 6
  • 14
0

you can use Launch4J or other java wrappers.

d_air
  • 631
  • 1
  • 4
  • 12
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.

Mathias Begert
  • 2,354
  • 1
  • 16
  • 26
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.
Community
  • 1
  • 1
dimo414
  • 47,227
  • 18
  • 148
  • 244