1

In my webapplication I want to allow administrators to execute system commands like:

        Process proc = Runtime
            .getRuntime()
            .exec("cmd.exe /C dir C:\\\"Program Files (x86)\"\\jboss-as-7.1.1.Final_JAX-RS_BookStore\\"+subDir);

I now retrieve the JBoss home directory via:

String SERVER_HOME = System.getenv("JBOSS_HOME");

Unfortunately, this returns me C:\Program Files (x86)\jboss-as-7.1.1.Final_JAX-RS_BookStore instead of: C:\\\"Program Files (x86)\"\\jboss-as-7.1.1.Final_JAX-RS_BookStore\\ so that the .exec(...) command won't work anymore.

How can I format this file path properly?

My-Name-Is
  • 4,814
  • 10
  • 44
  • 84
  • Read (and implement) *all* the recommendations of [When Runtime.exec() won't](http://www.javaworld.com/jw-12-2000/jw-1229-traps.html). Then ignore that it refers to `exec` and build the `Process` using a `ProcessBuilder`. Also break a `String arg` into `String[] args` to account for arguments which themselves contain spaces. – Andrew Thompson Jul 14 '13 at 18:40

1 Answers1

1

I believe the following should work:

String SERVER_HOME = "\"" + System.getenv("JBOSS_HOME") + "\"";

where the double quotes would allow the spaces within the path.

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • Oh ... thanks, it works! I thought, that I need the quotes before and after `Program Files (x86)` too. Thanks a bunch! – My-Name-Is Jul 14 '13 at 18:35