使用从 java 程序调用的 Windows 命令提示符将具有多个连续空格的字符串作为参数传递给 jar 文件
Pass a string with multiple contiguous spaces as a parameter to a jar file using Windows command prompt called from a java program
我想使用在另一个 java 程序中调用的 Windows 命令提示符将具有多个连续空格的字符串作为参数传递给 jar 文件。 java 文件是这样的,它打印了它的所有参数:
package src;
public class myClass
{
public static void main(String[] args)
{
for(int i = 0; i < args.length; i++)
{
System.out.println("args" + i+ ":" + args[i]);
}
}
}
现在,这就是我从另一个 java 程序调用上述主要方法并打印输出的方式:
package src;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class NewClass
{
public static void main(String[] args) throws IOException
{
Runtime rt = Runtime.getRuntime();
Process pr;
String grmmClassPath ="C:\Users\XX\Documents\NetBeansProjects\JavaApplication1\dist\JavaApplication1.jar";
String className = "src.myClass";
pr = rt.exec("cmd.exe /c java"
+ " -cp " + grmmClassPath
+ " " + className
+ " \"hello world\""
);
WriteProcessStream(pr);
}
public static void WriteProcessStream(Process pr) throws IOException
{
InputStreamReader isr = new InputStreamReader(pr.getInputStream());
String startLabel = "<OUTPUT>";
String endLabel = "</OUTPUT>";
BufferedReader br = new BufferedReader(isr);
String line = null;
System.out.println(startLabel);
while ((line = br.readLine()) != null)
{
System.out.println(line);
}
System.out.println(endLabel);
}
}
所以当我运行上面的程序时,它打印:
<OUTPUT>
arg 0 is: hello world
</OUTPUT>
这正是问题所在!我希望 args[0] 包含三个空格,但无论我做什么,我都无法获得至少包含两个连续空格的 args[0]。
有趣的是,如果我直接从 cmd.exe 调用 myClass 的主要方法,就像这样:
java -cp JavaApplication1.jar src.myClass "hello world"
我会得到以下输出:
arg 0 is:hello world
,令人惊讶的是,它的空间被预留了!
如果有人能帮助我,我将不胜感激。
Necro 但是:不要使用 Runtime.exec(String)
重载。 Per the javadoc (indirectly) it tokenizes the command at any whitespace 忽略如果您直接通过 CMD(或 Unix shell)输入此命令行将适用的引用规则。 Windows 执行程序然后从令牌重建命令行,并丢失额外的空格。
而是使用具有正确解析的 String[]
重载:
p = runtime.exec(new String[]{"cmd","/c","java","-cp",classpath,classname,"hello world"});
或者您实际上并未在此处使用 CMD 的任何功能,因此您不需要它:
p = runtime.exec(new String[]{"java","-cp",classpath,classname,"hello world"});
如果您改用 ProcessBuilder
,它的构造函数和 .command(setter)
将被声明为 String...
(可变参数),因此您无需编写 new String[]{...}
即可传递标记。
或者,运行 CMD 并将命令行作为 input:
p = runtime.exec("cmd"); // or new ProcessBuilder + start
String line = "java -cp " + classpath + " " + classname + " \"hello world\"\n";
p.getOutputStream.write(line.getBytes());
(对于这种方法,CMD 的输出将包括输入的横幅、提示和回显。)
我想使用在另一个 java 程序中调用的 Windows 命令提示符将具有多个连续空格的字符串作为参数传递给 jar 文件。 java 文件是这样的,它打印了它的所有参数:
package src;
public class myClass
{
public static void main(String[] args)
{
for(int i = 0; i < args.length; i++)
{
System.out.println("args" + i+ ":" + args[i]);
}
}
}
现在,这就是我从另一个 java 程序调用上述主要方法并打印输出的方式:
package src;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class NewClass
{
public static void main(String[] args) throws IOException
{
Runtime rt = Runtime.getRuntime();
Process pr;
String grmmClassPath ="C:\Users\XX\Documents\NetBeansProjects\JavaApplication1\dist\JavaApplication1.jar";
String className = "src.myClass";
pr = rt.exec("cmd.exe /c java"
+ " -cp " + grmmClassPath
+ " " + className
+ " \"hello world\""
);
WriteProcessStream(pr);
}
public static void WriteProcessStream(Process pr) throws IOException
{
InputStreamReader isr = new InputStreamReader(pr.getInputStream());
String startLabel = "<OUTPUT>";
String endLabel = "</OUTPUT>";
BufferedReader br = new BufferedReader(isr);
String line = null;
System.out.println(startLabel);
while ((line = br.readLine()) != null)
{
System.out.println(line);
}
System.out.println(endLabel);
}
}
所以当我运行上面的程序时,它打印:
<OUTPUT>
arg 0 is: hello world
</OUTPUT>
这正是问题所在!我希望 args[0] 包含三个空格,但无论我做什么,我都无法获得至少包含两个连续空格的 args[0]。
有趣的是,如果我直接从 cmd.exe 调用 myClass 的主要方法,就像这样:
java -cp JavaApplication1.jar src.myClass "hello world"
我会得到以下输出:
arg 0 is:hello world
,令人惊讶的是,它的空间被预留了!
如果有人能帮助我,我将不胜感激。
Necro 但是:不要使用 Runtime.exec(String)
重载。 Per the javadoc (indirectly) it tokenizes the command at any whitespace 忽略如果您直接通过 CMD(或 Unix shell)输入此命令行将适用的引用规则。 Windows 执行程序然后从令牌重建命令行,并丢失额外的空格。
而是使用具有正确解析的 String[]
重载:
p = runtime.exec(new String[]{"cmd","/c","java","-cp",classpath,classname,"hello world"});
或者您实际上并未在此处使用 CMD 的任何功能,因此您不需要它:
p = runtime.exec(new String[]{"java","-cp",classpath,classname,"hello world"});
如果您改用 ProcessBuilder
,它的构造函数和 .command(setter)
将被声明为 String...
(可变参数),因此您无需编写 new String[]{...}
即可传递标记。
或者,运行 CMD 并将命令行作为 input:
p = runtime.exec("cmd"); // or new ProcessBuilder + start
String line = "java -cp " + classpath + " " + classname + " \"hello world\"\n";
p.getOutputStream.write(line.getBytes());
(对于这种方法,CMD 的输出将包括输入的横幅、提示和回显。)