set windows PATH environment variable at runtime in Java
Asked Answered
M

4

6

I have a java program that fires off an executable using the Runtime.exec() method. I'm using the variant that takes in a set of command line params as one argument, and some environment variables as another argument.

The environment variable I'm tryign to set is path, so i'm passing in "PATH=C:\some\path". This does not work. Is there some trick to this or any alternatives. I am stuck to Java 1.4 unfortunately.

Mercurio answered 29/4, 2010 at 22:24 Comment(0)
R
7

Use getenv to get the environment and fix it up then use a flavour of exec to do the exec.

This works with a batch file that has path in it.

package p;

import java.util.*;

public class Run {
    static String[] mapToStringArray(Map<String, String> map) {
        final String[] strings = new String[map.size()];
        int i = 0;
        for (Map.Entry<String, String> e : map.entrySet()) {
            strings[i] = e.getKey() + '=' + e.getValue();
            i++;
        }
        return strings;
    }

    public static void main(String[] arguments) throws Exception {
        final Map<String, String> env = new HashMap<String, String>(System.getenv());
        env.put("Path", env.get("Path") + ";foo");
        final String[] strings=mapToStringArray(env);
        Runtime.getRuntime().exec("cmd /C start foo.bat",strings);
    }

}
Radiology answered 29/4, 2010 at 23:25 Comment(2)
none of these worked. I'm thinking you cannot change windows PATH variables for a process invoked with Runtime.execMercurio
works for me. if i mkdir foo and put a bar.bat in foo/ and change the exec to "cmd /C start bar.bat" i see a message from bar.bat using 1.6 on windoze 8.1Radiology
D
2

If "PATH=C:\some\path" appears in your source code, it would be incorrect as it would be trying to escape the 's' and 'p' in that string, you'd use "PATH=C:\\some\\path" instead (escaping the slashes). Also, you don't want to pass it in as a string directly, but as an array of strings (likely with that as the only string in it).

Dacha answered 29/4, 2010 at 22:33 Comment(0)
P
2

If you want to change the Path variable on windows, you should take a look at JNI_Registry: http://www.trustice.com/java/jnireg/

It's a Java binding to the Windows Registry API and comes with a very small footprint. I have used it for my current project and it works just fine.

Parthenogenesis answered 31/8, 2010 at 10:53 Comment(0)
O
1

One solution might be to add an additional command to "exec" where you set the path ... as in the example found here: http://www.neowin.net/forum/topic/620450-java-runtimegetruntimeexec-help/

excerpt:

            cmd = new String[7];
            cmd[0] = "cmd"; 
            cmd[1] = "/C";
            cmd[2] = "set PATH=C:\\Program Files\\Java\\jdk1.6.0_04\bin";
            cmd[3] = "copy " + "\"" +path + "\\" +name+ "\"" + " C:\\java";
            cmd[4] = "chdir C:\\java";
            cmd[5] = "javac *.java";
            cmd[6] = "jar cmf mainClass.txt"+" name"+".jar *.class";

            try{
            Runtime.getRuntime().exec(cmd);
Osiris answered 16/10, 2011 at 22:3 Comment(1)
is that accepted?Bautram

© 2022 - 2024 — McMap. All rights reserved.