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.