how to read value of environment variable

1

195.229.241.2

Hi,
I would like to read system environment variables like path etc
from inside a java program without using the java -D option.
Is it possible ?
TIA
sarath
 
M

Marek Lange

195.229.241.2 said:
Hi,
I would like to read system environment variables like path etc
from inside a java program without using the java -D option.
Is it possible ?

String envValue = System.getProperty(key);

-marek
 
J

Joseph Millar

String envValue = System.getProperty(key);

That does not give environment variables, only JVM system
properties from the System.getProperties() object.

To get outside environment variables, other than using -D
to pass them in, you have basically 2 choices.

1. Call to external code like a native method written in C/C++.
You can also call into Perl and Python, etc using various
3rd party packages. Plenty of examples around for calling
getenv() in C from a native method.

2. exec() a process the dumps the environment variables to
output, then read that output from Java. There are examples
of this sort floating around.

Both of these aproaches are going to be platform dependent,
for different reasons.

--Joe
 
T

Thomas Weidenfeller

195.229.241.2 said:
I would like to read system environment variables like path etc
from inside a java program without using the java -D option.
Is it possible ?

-D is a good idea. But if you want something else, you can try to:

- use the deprecated System.getenv()

- use a hack where you exec to get a list of all environment variables
(platform dependent) and parse that list using Properties.load().
This is gambling that the environment variable format matches the
format for Properties - which it does on some platforms - at least
mostly.

/Thomas
 
J

Jon A. Cruz

Thomas said:
- use the deprecated System.getenv()

No.

On most VM's that will just throw an exception. Might even kill his
program if it's not expected. On anything later than 1.0, it will not work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top