" for cmd file from java In cmd.exe, I try this: echo " read "book "" Mistake: The system cannot find the specified fi...">

Escape "<>" for cmd file from java

In cmd.exe, I try this:

echo " read "book <init>""

Mistake:

The system cannot find the specified file.

He works with

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

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

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

How can I avoid <> 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!");
    }
}
+4
source share
1 answer

I think your question is:


"How do I pass an argument to a bat file that contains several quotation marks and special characters in it so that the bat file does not interpret it?"


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

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

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

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

Test in cmd:

echo " read "book <init>""

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

Source: https://habr.com/ru/post/1606391/


All Articles