my dog learned polymorphism*
The moose likes Java in General and the fly likes Using JAVA to read environmental variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Using JAVA to read environmental variables" Watch "Using JAVA to read environmental variables" New topic
Author

Using JAVA to read environmental variables

igwe kalu kalu ogba
Ranch Hand

Joined: Feb 03, 2005
Posts: 133
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

Joined: Oct 29, 2001
Posts: 8914
    
    8

String jbossHome = System.getProperty("JBOSS_HOME");


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
igwe kalu kalu ogba
Ranch Hand

Joined: Feb 03, 2005
Posts: 133
thanks
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

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.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
 
GeeCON Prague 2014
 
subject: Using JAVA to read environmental variables