Java命令行参数中的空格 [英] Space in Java command-line arguments

查看:476
本文介绍了Java命令行参数中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Java命令行参数中,空格后的任何字符都会被忽略。例如,

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

java test.AskGetCampaignByName "Dummy books"

我将第一个参数(args [0])仅作为Dummy。单引号也没有帮助。
有解决方法/解决方法吗?可能是因为我的终端设置?

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?

我的$ TERM是xterm,而$ LANG是en_IN。

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

推荐答案

参数由shell处理(我假设你在Linux下使用Bash?),所以任何终端设置都不应该影响这个。

The arguments are handled by the shell (I assume you are using Bash under Linux?), so any terminal settings should not affect this.

既然你已经引用了这个论点,它就应该有效了。我能想到的唯一可能的解释是,如果你的 java 命令是一个包装器脚本,并且在传递给真实程序时会弄乱参数的转义。这很容易做,或者可能有点难以正确执行。

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.

正确的包装脚本应该将其所有参数传递为 $ {1 +$ @} ,任何其他版本很可能是一个关于能够正确处理嵌入空间的错误。这种做法并不罕见,但是任何出现的 $ 2 或类似事件都很麻烦,必须写成$ 2(或者可能 $ {2 +$ 2} )为了正确处理嵌入空间,这是犯了很多罪。

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.

不太直观的语法 $ {1 +$ @} 的原因是原来的 $ * 将所有参数加入为$ 1 $ 2 $ 3 ...,这对于嵌入式空间不起作用。然后引入$ @(正确)扩展到$ 1$ 2$ 3...... 对于所有参数,如果没有给出参数,它应该扩展为空。不幸的是,一些Unix供应商搞砸了并且使$ @扩展到,即使没有参数,为了解决这个问题,发明了 $ {1 +$ @} 的聪明(但不那么可读)黑客,使得$ @ 仅在参数 $ 1 设置时展开(即在没有参数的情况下避免扩展)。

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).

如果我的包装器假设错误,您可以尝试使用 strace 进行调试:

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

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

并找出传递给 execve 的参数。运行 strace / bin / echo'1 2'3 的例子:

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)
...

这篇关于Java命令行参数中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆