Java 命令行参数中的空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/743454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Space in Java command-line arguments
提问by ashweta
In my Java command-line arguments, any characters after space get ignored. For example,
在我的 Java 命令行参数中,空格后的任何字符都会被忽略。例如,
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?
我只将第一个参数 (args[0]) 设为“Dummy”。单引号也无济于事。是否有解决方法/修复方法?可能是因为我的终端设置?
My $TERM is xterm, and $LANG is "en_IN".
我的 $TERM 是 xterm,而 $LANG 是“en_IN”。
采纳答案by hlovdal
The arguments are handled by the shell (I assume you are using Bash under Linux?), so any terminal settings should not affect this.
参数由 shell 处理(我假设您在 Linux 下使用 Bash?),因此任何终端设置都不应影响这一点。
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.
既然你已经引用了这个论点,它应该有效。我能想到的唯一可能的解释是,如果您的java
命令是一个包装脚本,并且在传递给实际程序时会弄乱参数的转义。这很容易做到,或者可能有点难以正确执行。
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.
正确的包装器脚本应该将其所有参数传递给 as ${1+"$@"}
,任何其他版本很可能是关于能够正确处理嵌入空间的错误。正确执行这种情况并不少见,但是任何出现的$2
或类似的情况也很麻烦,必须写为"$2"
(或可能${2+"$2"}
)以正确处理嵌入空间,这是有罪的。
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).
不那么直观的语法的原因${1+"$@"}
是原始$*
加入了所有参数,因为"$1 $2 $3 ..."
它不适用于嵌入空间。然后"$@"
被引入(正确)扩展"$1" "$2" "$3" ...
为所有参数,如果没有给出参数,它应该扩展为空。不幸的是一些Unix厂商搞砸并提出"$@"
扩大到""
即使在没有参数的情况下,并且要解决这个聪明的(但不是那么可读)写的黑客${1+"$@"}
被发明,使得"$@"
仅在参数扩展$1
设定(即避免的情况下,扩张没有论据)。
If my wrapper assumption is wrong you could try to debug with strace:
如果我的包装器假设是错误的,您可以尝试使用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
。运行“ 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)
...
回答by nxadm
回答by Thorbj?rn Ravn Andersen
It 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.
听起来您正在使用操作系统发行版,其中java
用户可用的命令是一个包装器,它可以在“某处”找到正确的 JVM 并相应地调用它。
If so, it most likely does not escape the arguments properly when invoking the actual java
executable.
如果是这样,它很可能在调用实际java
可执行文件时没有正确转义参数。
What distribution do you use?
你使用什么发行版?
回答by cpugamerbb
Just reassemble the arguments in your Java program:
只需在您的 Java 程序中重新组合参数:
StringBuilder allArgs = new StringBuilder();
for (int i=0; i < args.length; i++)
{
//System.out.println("arg"+i+": "+args[i]);
allArgs.append(args[i]+" ");
}
// Parse out the args the way you wish using allArgs
回答by Zarathustra
You just have to escape the spaces like this:
你只需要像这样逃避空间:
normal String: "Hello World!"
escaped String: "Hello" "World!"
That worked for me.
那对我有用。
My environment:
我的环境:
23:39:19 Zarathustra@thora:/Users/Zarathustra~$bash -version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin11)
Copyright (C) 2007 Free Software Foundation, Inc.