1

I am trying to run few .bat commands using Runtime exec(Java). Issue is that after setting the environment variables, I am not able to use these variables.

Runtime rt = Runtime.getRuntime();
Process pr;
pr = rt.exec("cmd.exe /c echo %JAVA_HOME%");
InputStream is;
InputStreamReader isr;
BufferedReader reader ;
String line;
is = pr.getInputStream();
isr = new InputStreamReader(is);
reader = new BufferedReader(isr);

while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
pr.getOutputStream().close();`

JAVA_HOME doesnt return the actual env path.

morgano
  • 17,210
  • 10
  • 45
  • 56
user2640248
  • 393
  • 1
  • 4
  • 14
  • It works perfectly for me how exactly you are trying to run this code? Are you using Eclipse or any other GUI tool for running ? Can you show me the output of command "cmd.exe /c echo %JAVA_HOME%" from command line – Xinus Aug 13 '13 at 01:50
  • Can you share the code you're using in your bat files? – morgano Aug 13 '13 at 01:53
  • @Xinus I was asking the OT, not you ;-) – morgano Aug 13 '13 at 02:06
  • oh.. deleted the comment :) – Xinus Aug 13 '13 at 02:28
  • Read (and implement) *all* the recommendations of [When Runtime.exec() won't](http://www.javaworld.com/jw-12-2000/jw-1229-traps.html). That might solve the problem. If not, it should provide more information as to the reason it failed. 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 Aug 13 '13 at 08:36

2 Answers2

1

The code worked perfectly for me,

try {
            Runtime rt = Runtime.getRuntime();
            Process pr;
            pr = rt.exec("cmd.exe /c echo %JAVA_HOME%");
            InputStream is;
            InputStreamReader isr;
            BufferedReader reader;
            String line;
            is = pr.getInputStream();
            isr = new InputStreamReader(is);
            reader = new BufferedReader(isr);

            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            reader.close();
            pr.getOutputStream().close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

If it doesn't work for you,

  1. make sure you have environment variable %JAVA_HOME% correctly defined. Start command prompt and share output of cmd.exe /c echo %JAVA_HOME%
  2. Do you use any IDE for running the program?
Xinus
  • 29,617
  • 32
  • 119
  • 165
  • output of command "cmd.exe /c echo %JAVA_HOME%" from command prompt returns "C:\Program Files\Java\jdk1.7.0_25\", however when executed from Eclipse IDE using above mentioned code, it return as "%JAVA_HOME%" (echo's the variable name) – user2640248 Aug 14 '13 at 00:35
  • Observed that, few env variables are not being returned , which includes JAVA_HOME and other custom set env variables. However env variables such as USERNAME, PATH, DIR, TEMP are being returned with correct value. NOTE : Logged in as administrator – user2640248 Aug 14 '13 at 00:41
  • I tried to read the available env variables in Eclipse IDE,with command "rt.exec("cmd.exe /c SET ")", and observed that not all env variables shown in system properties is listed in the given output. However same command executed from command prompt lists all env variables . – user2640248 Aug 14 '13 at 00:57
  • It seems like eclipse is not inheriting all the environment variables, you can try couple of things, 1. Try starting the eclipse from command line by navigating to eclipse directory and check if its able to recognize the env, 2. Set the JAVA_HOME explicitly in eclipse run configuration (run as->Run Configuration->Environment->[Add] button) – Xinus Aug 14 '13 at 02:23
  • Very true, after restarting eclipse, all env variables are retained. My requirement is that, My test would run a .bat file which would set a custom env variable and use that env variable throughout the program . So how would I make this possible ?? NOTE: Even if I execute the .bat file from command prompt,it needs a new cmd prompt for env variable to be effective – user2640248 Aug 15 '13 at 23:33
  • My guess is that your bat file will set all the required environment and start your program ? yes you can do that. you do not require command prompt to run that bat file, you can directly launch it from Windows file explorer by double clicking the bat file. – Xinus Aug 16 '13 at 04:05
  • how do I do this programitically ?? I want this to be automated, invoking .bat and using env variables in subsequent tests – user2640248 Aug 16 '13 at 18:31
  • It is not possible to update the environment directlt but still you can refer to answer from pushy http://stackoverflow.com/questions/318239/how-do-i-set-environment-variables-from-java – Xinus Aug 17 '13 at 03:01
0

You can set the JAVA_HOME environment variable to ensure the correct JVM is used.
Use ProcessBuilder to set the JAVA_HOME environment variable and execute the command.

ProcessBuilder pb = new ProcessBuilder();
// copy the java.home variable from the current Java process (or set your own)
pb.environment().put("JAVA_HOME", System.getProperty("java.home"));
pb.command("cmd", "/c", "echo %JAVA_HOME%");

Process process = pb.start();
try (BufferedReader processOutput = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
    String outputLine;
    while ((outputLine = processOutput.readLine()) != null) {
        System.out.println(outputLine);
    }
}
R. Oosterholt
  • 7,720
  • 2
  • 53
  • 77