Search code examples
javaenvironment-variablesstring-formattingfile-locationcommand-execution

How do I get formatted value of environment variable


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?


Solution

  • I believe the following should work:

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

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