4

In cmd.exe I try this :

echo " read "book <init>""

The error is :

The system cannot find the file specified.

It works with

echo "read "book ^<init^>""

But if I want to send as argument from java, it will not work:

String s4 = "read \"book ^<init^> \"" ;

How can I escape < > for cmd from java?

Full code :

    public static void main(String[] args) throws IOException, InterruptedException{

    List<String> paramsArray = new ArrayList<String>();

    String s = "read \"book ^<init^> \"" ;
    paramsArray.add("exec.cmd"); //cmd file that only has echo %*
    paramsArray.add(s);

    ProcessBuilder process = new ProcessBuilder(paramsArray).inheritIO(); 

    Process p = process.start();

    int exitStatus = p.waitFor();

    System.out.println("exitStatus is " + exitStatus);

    if (exitStatus == 0)
    {
      System.out.println("Ok!");
    }
    else{

        System.out.println("Not ok!");
    }
}
Jimmy
  • 43
  • 3
  • Why do you wish to use the stream redirection characters on the command line? – Kayaman Sep 08 '15 at 10:34
  • @Kamayan , I am sorry , I do not understand the question . My problem is that I pass some arguments to a bat file from another file that look like "revert this: " eu.popin.tm." " . And I need to use this argument . But I got this error with system cannot find the file specified. I do not understand how cmd interprets "< >" – Jimmy Sep 08 '15 at 10:41
  • The `>` character will redirect the output of a command to a file. The `<` character will redirect a file as input to a command. Those are very poor choices of characters to handle on the command line. – Kayaman Sep 08 '15 at 10:44
  • @Kayaman , I understand now . I want that argument to be treated as a string , from the first " to the last one . How can I do this? – Jimmy Sep 08 '15 at 10:47

1 Answers1

3

I think your question is :


"How can I pass an argument to a bat file that contains multiple quotes and special characters in it , such that the bat file will not interpret it ?"


You can replace the double quotes ("") with single quotes in Java :

your_string= your_string.replace("\"", "'");

But better you can put ^ in front of your string , which will be :

 String s = "^" +"read \"book ^<init^> \"" ;

Test in cmd:

echo " read "book <init>""

echo  ^" read "book <init>""
Viva
  • 1,983
  • 5
  • 26
  • 38