Pass a string with multiple contiguous spaces as a parameter to a jar file using Windows command prompt called from a java program

让人想犯罪 __ 提交于 2019-12-10 09:57:30

问题


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

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>

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.

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"

I would have had the following output:

arg 0 is:hello   world

, and surprisingly, its spaces had been reserved!

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


回答1:


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.

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

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

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

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[]{...}.

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

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



来源:https://stackoverflow.com/questions/31195623/pass-a-string-with-multiple-contiguous-spaces-as-a-parameter-to-a-jar-file-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!