Solved

How to access environment variable from java class

Posted on 2002-04-10
12
252 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
Industry Leaders: 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!

 
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

Suggested Solutions

Title # Comments Views Activity
How to execute a Python program and gather return output in Java 2 49
by zero exception 10 70
sql import cannot be resolved jsp 3 75
collection output issue 9 64
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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

733 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