Pass a string with multiple contiguous spaces as a parameter to the jar file using windows command line invoked from java program
I want to pass a string with multiple adjacent spaces as a parameter to a jar file using a windows command line 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 I am calling the main method from another java program that way and printing 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>
Exactly where the problem is! I want args [0] with three spaces, but whatever I do, I cannot get args [0] with at least two contiguous spaces.
I wonder what if I called the main method myClass directly from cmd.exe, like this:
java -cp JavaApplication1.jar src.myClass "hello world"
I would have the following output:
arg 0 is:hello world
and it's amazing that its spaces have been reserved!
I would be grateful if anyone can help me.
source to share
Necro, but: don't use overloading Runtime.exec(String)
. In the javadoc (indirectly) it tokenizes the command to any spaces , ignoring the quoting rules that would apply if you entered that command line directly through the CMD (or Unix shell). The Windows executor then recovers the command line from the tokens, losing the extra spaces.
Use overloading String[]
with correct parsing instead :
p = runtime.exec(new String[]{"cmd","/c","java","-cp",classpath,classname,"hello world"});
or you are not actually using any CMD function here, so you don't need this:
p = runtime.exec(new String[]{"java","-cp",classpath,classname,"hello world"});
If you use ProcessBuilder
its ctor and is .command(setter)
declared as String...
(vararg), so you can just pass the tokens without writing new String[]{...}
.
Alternatively, start CMD and enter the command at the command prompt:
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 CMD output will include a banner, tooltip, and input echo.)
source to share