• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

How to access environment variable from java class


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
saurav17
Asked:
saurav17
  • 5
  • 2
  • 2
  • +2
1 Solution
 
objectsCommented:
Perfect solution: there is none :-(
The only way to access environment vars is by using platform dependent solution using JNI or exec().
0
 
msterjevCommented:
If you are running on Windows I can give you windows solution!
0
 
objectsCommented:
exec("cmd /c set");
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
msterjevCommented:
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
 
objectsCommented:
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
 
objectsCommented:
Another option would be to pass the environment variable value when you startup the JVM:

java -Dmyenv=$MYENV myclass
0
 
Venci75Commented:
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
 
heyhey_Commented:
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
 
objectsCommented:
> passes these variables to your Java program on startup.

Already mentioned that :-)
0
 
heyhey_Commented:
sorry, I missed it :)
0
 
Venci75Commented:
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
 
moduloCommented:
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now