Solved

How to access environment variable from java class

Posted on 2002-04-10
12
258 Views
Last Modified: 2010-03-31

Hi pals,

I have an serious problem. My java program has to get
environment variables from the system. But how,
I tried lots of methods, but ended in vain.

I'm not expecting suggestions, as i tried a lot but i need a perfect solution.

And guys I thank u al in advance.


Regards,
K.J.S.
0
Comment
Question by:saurav17
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +2
12 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6933064
Perfect solution: there is none :-(
The only way to access environment vars is by using platform dependent solution using JNI or exec().
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6933085
If you are running on Windows I can give you windows solution!
0
 
LVL 92

Expert Comment

by:objects
ID: 6933098
exec("cmd /c set");
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 3

Expert Comment

by:msterjev
ID: 6933269
I mean more sofisticated solution,something like this:

String path=System.Utils.getEnvironmentVariable("PATH");

or,

Enumeration e=System.Utils.getEnvironmentVariables();

saurav17 can decide which one he want to use!
On the other side, last weak I wrote several such solutions for nothing.Not thanks,saurav17 you can use Runtime.getRuntime().exec("cmd /c set")!
0
 
LVL 92

Expert Comment

by:objects
ID: 6933284
Could certainly wrap the exec call in a method to make it more 'sophisticated' :-)

> solutions for nothing.Not thanks

Get used to it, it happens all the time these days.
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 6933286
Another option would be to pass the environment variable value when you startup the JVM:

java -Dmyenv=$MYENV myclass
0
 
LVL 9

Expert Comment

by:Venci75
ID: 6933306
As objects said - ther is no perfect solution to this problem. This works on windows and unix, but may be there are systems on which this won't work:
public String getEnvVar(String property) throws IOException {
  String command;
  String os = System.getProperty("os.name");
  if (os.startsWith("Windows")) {
      command = "cmd.exe /C SET " + property;
  } else { // assume that the OS is unix
      command = "echo $" + property;
  }
  Process p = Runtime.getRuntime().exec(command);
  BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
  String line = reader.readLine();
  return line.substring(line.indexOf('=')+1);
}



What kind of environment variables do you need. Some of the values of these variables could be accessed by calling System.getProperty();
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6933360
you can write a simple .bat file / .sh script that passes these variables to your Java program on startup.


> Get used to it, it happens all the time these days.

hehe
0
 
LVL 92

Expert Comment

by:objects
ID: 6933368
> passes these variables to your Java program on startup.

Already mentioned that :-)
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6933372
sorry, I missed it :)
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7940543
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by: objects
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venci75
EE Cleanup Volunteer
0
 

Expert Comment

by:modulo
ID: 7990735
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question