• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using JAVA to read environmental variables

 
igwe kalu kalu ogba
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, i've written a java program to read environmental variables.

I am using :

String bleh = "";

Process p = Runtime.getRuntime().exec("cmd.exe /c echo %JBOSS_HOME%");
BufferedReader br = new BufferedReader
( new InputStreamReader( p.getInputStream() ) );

bleh = br.readLine();

System.out.println(bleh);

The problem I have is this, sometimes this code simply stops working and returns null. Is there a way to get this value all the time? Maybe using JNI or something. A fail safe method of reading an environmental variable. Thanks a lot.
 
Joe Ess
Bartender
Posts: 9313
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String jbossHome = System.getProperty("JBOSS_HOME");
 
igwe kalu kalu ogba
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just note that even this is not failsafe, as starting the program with a command like:

java -D JBOSS_HOME=C:\some\random\directory ....

will override the system variable. But I think that this is the best you're going to be able to get.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic