java - Not able to access environment variables using runtime exec -
i trying run few .bat commands using runtime exec(java). issue after setting environment variables, not able 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; = 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 actual env path.
the code worked 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; = 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 doesn't work you,
- make sure have environment variable %java_home% correctly defined. start command prompt , share output of
cmd.exe /c echo %java_home%
- do use ide running program?
Comments
Post a Comment