Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8155079

Java Runtime Exec fails if the path has Parentheses

XMLWordPrintable

    • generic
    • windows

      FULL PRODUCT VERSION :
      java version "1.8.0_77"
      Java(TM) SE Runtime Environment (build 1.8.0_77-b03)
      Java HotSpot(TM) 64-Bit Server VM (build 25.77-b03, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 10.0.10586]

      A DESCRIPTION OF THE PROBLEM :
      Java Runtime Exec fails if the path has parantheses. Consider the following sample code:

      import java.io.IOException;

      public class SampObject {

          String command;

          public void launchBatch() {
              String cmdArray[];

              command = "C:\\test(1)folder\\Launch.bat";

              cmdArray = new String[] {
                  "cmd",
                  "/c",
                  "start",
                  command
              };

              try {
                  Runtime.getRuntime().exec(cmdArray);
                  System.out.println("Batch file launched");
              } catch (IOException e) {
                  System.out.println("Error launching file: " + e.toString());
              }
          }
      }

      public class Sample {

      public static void main(String[] args) {
          SampObject sampObj = new SampObject();
          sampObj.launchBatch();
      }
      }

      I get the following error:

      'C:\test' is not recognized as an internal or external command, operable program or batch file.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run this sample code and it fails right away:

      import java.io.IOException;

      public class SampObject {

          String command;

          public void launchBatch() {
              String cmdArray[];

              command = "C:\\test(1)folder\\Launch.bat";

              cmdArray = new String[] {
                  "cmd",
                  "/c",
                  "start",
                  command
              };

              try {
                  Runtime.getRuntime().exec(cmdArray);
                  System.out.println("Batch file launched");
              } catch (IOException e) {
                  System.out.println("Error launching file: " + e.toString());
              }
          }
      }

      public class Sample {

      public static void main(String[] args) {
          SampObject sampObj = new SampObject();
          sampObj.launchBatch();
      }
      }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expect the batch file to run when I run the code mentioned above. Instead I get the following error:

      'C:\test' is not recognized as an internal or external command, operable program or batch file.
      ACTUAL -
      'C:\test' is not recognized as an internal or external command, operable program or batch file.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      'C:\test' is not recognized as an internal or external command, operable program or batch file.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.IOException;

      public class SampObject {

          String command;

          public void launchBatch() {
              String cmdArray[];

              command = "C:\\test(1)folder\\Launch.bat";

              cmdArray = new String[] {
                  "cmd",
                  "/c",
                  "start",
                  command
              };

              try {
                  Runtime.getRuntime().exec(cmdArray);
                  System.out.println("Batch file launched");
              } catch (IOException e) {
                  System.out.println("Error launching file: " + e.toString());
              }
          }
      }

      public class Sample {

      public static void main(String[] args) {
          SampObject sampObj = new SampObject();
          sampObj.launchBatch();
      }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The only solution I have is to not use any folder name with parentheses.

            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: