views:

113

answers:

3

Hello Guys,

I want to make a slick java commandline app which doesnt include all the nasty "java -jar some.jar arguments"

instead, I would have it work just like

program -option argument

like any other commandline app. I use ubuntu linux, and it would be fine if it included a bit of .sh script or anything. I know I can just create a file with java -jar program.jar and do chmod +x file, afterwards I could run i with ./file, but then how can I pass the arguments to the program ?

+10  A: 

Use

java -jar program.jar $1 $2 $3

to access 1st, 2nd and 3rd argument of the script.

Example from http://www.ibm.com/developerworks/library/l-bash2.html

#!/usr/bin/env bash

echo name of script is $0
echo first argument is $1
echo second argument is $2
echo seventeenth argument is $17
echo number of arguments is $#

Or, even better, do as @nos suggests:

java -jar program.jar "$@"
aioobe
Or just forward all arguments to the java app . e.g. `java -jar $@`
nos
if you'd like, place the bash script in the bash PATH
phoenix24
Just to be really picky, I think "$@" handles quoting better (quotes each argument, which is handy if there are spaces in paths, say). man bash would tell you for certain...
pdbartlett
Cool :) thanks guys
Felix
@pdbartlett, could you elaborate?
aioobe
Even better, use `java -jar program.jar "$@"`. The quotes keep quoting so your parameters can also have spaces.
Kevin Brock
If you were calling `script.sh "hello world" blah blah`:`$@` would expand to: `hello world blah blah` whereas`"$@"` would expand to: `"hello world" "blah" "blah"`
pdbartlett
Thanks @pdbartlett for the clear example! Updated the answer. (And set it as wiki)
aioobe
A: 

On Linux you can use binfmt.

binary_runner
A: 

If you're trying to be really slick and you are running on a platform that supports bash, you can hide the fact that you're using Java completely from the user by producing a self extracting archive.

Take a look at this post for some hints on how you to implement a self extracting archive.

Atonewell