使用从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

查看:175
本文介绍了使用从java程序调用的Windows命令提示符,将具有多个连续空格的字符串作为参数传递给jar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用在另一个java程序中调用的Windows命令提示符将带有多个连续空格的字符串作为参数传递给jar文件。 java文件就是这样打印它的所有参数:

I want to pass a string with multiple contiguous spaces as a parameter to a jar file using Windows command prompt called in another java program. The java file is something like this which prints all of its arguments:

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程序调用上面的main方法并打印输出:

Now, this is how I call the above main method from another java program and print the output:

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);
    }
}

因此,当我运行上述程序时,它会打印:

So when I run the above program, It prints:

<OUTPUT>
arg 0 is: hello world
</OUTPUT>

这正是问题所在!我希望args [0]有三个空格,但是我做的任何事情,我都不能得到至少有两个连续空格的args [0]。

That's exactly where the problem is! I want the args[0] to be with three spaces, but anything I do, I can't get args[0] with at least two contiguous spaces.

它是有趣的是,如果我直接从cmd.exe调用myClass的main方法,就像这样:

It's interesting that If I'd called the myClass' main method directly from cmd.exe, like this:

java -cp JavaApplication1.jar  src.myClass "hello   world"

我会得到以下输出:

arg 0 is:hello   world

,令人惊讶的是,它的空间已被保留!

, and surprisingly, its spaces had been reserved!

如果有人能帮助我,我会很感激。

I'd appreciate it if anyone could help me with this.

推荐答案

Necro但是:不要使用 Runtime.exec(String)重载。 每个javadoc (间接)它将命令标记为任何空格忽略了直接通过CMD(或Unix)输入此命令行时适用的引用规则贝壳)。然后Windows执行程序从令牌重建命令行,丢失额外的空格。

Necro but: don't use the Runtime.exec(String) overload. Per the javadoc (indirectly) it tokenizes the command at any whitespace ignoring the quoting rules that would apply if you entered this command line directly via CMD (or a Unix shell). The Windows executor then rebuilds the command line from the tokens, with your extra spaces lost.

而是使用 String [] 使用正确的解析重载:

Instead use the String[] overload with the correct parsing:

 p = runtime.exec(new String[]{"cmd","/c","java","-cp",classpath,classname,"hello   world"});

或者您实际上并未使用CMD的任何功能,因此您不需要它:

or you don't actually use any feature of CMD here so you don't need it:

 p = runtime.exec(new String[]{"java","-cp",classpath,classname,"hello   world"});

如果您使用 ProcessBuilder ,而不是它的ctor和。命令(setter)被声明为 String ... (vararg)所以你可以在不写 new的情况下传递令牌String [] {...}

If you use ProcessBuilder instead, its ctor and .command(setter) are declared as String... (vararg) so you can just pass the tokens without writing new String[]{...}.

或者,运行CMD并将命令行作为输入

Alternatively, run CMD and give it the commandline as input:

 p = runtime.exec("cmd"); // or new ProcessBuilder + start 
 String line = "java -cp " + classpath + " " + classname + " \"hello   world\"\n";
 p.getOutputStream.write(line.getBytes());

(对于这种方法,CMD的输出将包括输入的横幅,提示和回显。)

(For this approach the output from CMD will include banner, prompt, and echo of the input.)

这篇关于使用从java程序调用的Windows命令提示符,将具有多个连续空格的字符串作为参数传递给jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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