tags:

views:

2183

answers:

4

In my Java command-line arguments, any characters after space get ignored. For example,

java test.AskGetCampaignByName "Dummy books"

I get first argument (args[0]) as "Dummy" only. Single quotes also do not help. Does anybody know any workaround/fix for this? Could it be because of my terminal settings? My $TERM is xterm, and $LANG is "en_IN".

A: 

In case your program needs more than positional arguments (= when the command line usage is important), you should consider options and switches. Apache Commons has a great library for this.

A: 

Sounds like you are using a operating system distribution where the java command available to the user is a wrapper which finds the right JVM "somewhere" and invokes it accordingly.

If so, it most likely does not escape the arguments properly when invoking the actual java executable.

What distribution do you use?

Thorbjørn Ravn Andersen
+5  A: 

The arguments are handled by the shell (I assume you are using bash under linux?), so any terminal settings should not affect this. Since you already have quoted the argument it ought to work. The only possible explanation I can think of is if your java command is a wrapper script and messes up the escaping of the arguments when passing on to the real program. This is easy to do, or perhaps a bit hard to do correctly.

A correct wrapper script should pass all its arguments on as ${1+"$@"}, any other version is most likely a bug with regards to being able to handle embedded spaces properly. This is not uncommon to do properly, however also any occurrences of $2 or similar are troublesome and must be written as "$2" (or possibly ${2+"$2"}) in order to handle embedded spaces properly, and this is sinned against a lot.

The reason for the not so intuitive syntax ${1+"$@"} is that the original $* joined all arguments as "$1 $2 $3 ..." which did not work for embedded spaces. Then "$@" was introduced that (correctly) expanded to "$1" "$2" "$3" ... for all parameters and if no parameters are given it should expand to nothing. Unfortunately some unix vendor messed up and made "$@" expand to "" even in case of no arguments, and to work around this the clever (but not so readable) hack of writing ${1+"$@"} was invented, making "$@" only expand if parameter $1 is set (i.e. avoiding expansion in case of no arguments).

If my wrapper assumption is wrong you could try to debug with strace

strace -o outfile -f -ff -F java test.AskGetCampaignByName "Dummy books"

and find out what arguments are passed to execve. Example from running "strace /bin/echo '1 2' 3"

execve("/bin/echo", ["/bin/echo", "1 2", "3"], [/* 93 vars */]) = 0
brk(0)                                  = 0x2400000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f420075b000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f420075a000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/usr/lib64/alliance/lib/tls/x86_64/libc.so.6", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/alliance/lib/tls/x86_64", 0x7fff08757cd0) = -1 ENOENT (No such file or directory)
open("/usr/lib64/alliance/lib/tls/libc.so.6", O_RDONLY) = -1 ENOENT (No such file or directory)
...
hlovdal
Thanks a lot hlovdal!That was exactly the case. We generate an executable through ant build that sets the classpaths etc. And we were using "java @" in the script. Substituting your ${1 + "@"} solved the problem. Sorry for misleading by using java directly in my example. I assumed it means the same
ashweta
A: 

This is not an answer but wouldn't fit into a comment:

On my Windows machine "hello world" "hello world" "hello world" passed as command-line arguments will be picked up as args[0 (/1) (/2)] == "hello world" and on Linux this simply does not work (i don't remember what it does do, as I wasn't too concerned). But I would be glad to see any solution to this. Thanks for the question.

@Thorbjørn Ravn Andersen, please elaborate. How can I change the way JVM is invoked?

Peter Perháč