Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Space in Java command-line arguments

Tags:

java

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

java test.AskGetCampaignByName "Dummy books" 

I get the first argument (args[0]) as "Dummy" only. Single quotes also do not help. Is there a workaround/fix for this? Could it be because of my terminal settings?

My $TERM is xterm, and $LANG is "en_IN".

like image 812
ashweta Avatar asked Apr 13 '09 09:04

ashweta


People also ask

How do you put a space in a command line argument?

In the Command Prompt, the caret character ( ^ ) will let you escape spaces—in theory. Just add it before each space in the file name.

How do you pass spaces in Java?

You can put double-quotes around an argument in a java command, and that will allow you to pass arguments with spaces in them.


1 Answers

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+"$@"}, and 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) ... 
like image 112
hlovdal Avatar answered Oct 09 '22 02:10

hlovdal