Solved

How to access environment variable from java class

Posted on 2002-04-10
12
259 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

617 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