Solved

How to access environment variable from java class

Posted on 2002-04-10
12
238 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
  • 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 80
Checkbox and ListView in Android Layout 4 65
Java Timer (static) 9 43
web application structure 18 77
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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

772 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