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,

  1. make sure have environment variable %java_home% correctly defined. start command prompt , share output of cmd.exe /c echo %java_home%
  2. do use ide running program?

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -