Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Windows system variables in Java 1.4

What is the best/foolproof way to get the values of environment variables in Windows when using J2SE 1.4 ?

like image 305
Saravanan M Avatar asked Feb 27 '26 18:02

Saravanan M


2 Answers

You can use getEnv() to get environment variables:

String variable = System.getenv("WINDIR");  
System.out.println(variable);  

I believe the getEnv() function was deprecated at some point but then "undeprecated" later in java 1.5

ETA:

I see the question now refers specifically to java 1.4, so this wouldn't work for you (or at least you might end up with a deprecation warning). I'll leave the answer here though in case someone else stumbles across this question and is using a later version.

like image 174
Eric Petroelje Avatar answered Mar 02 '26 07:03

Eric Petroelje


There's a switch to the JVM for this. From here:

Start the JVM with the "-D" switch to pass properties to the application and read them with the System.getProperty() method.

SET myvar=Hello world
SET myothervar=nothing
java -Dmyvar="%myvar%" -Dmyothervar="%myothervar%" myClass

then in myClass

String myvar = System.getProperty("myvar");
String myothervar = System.getProperty("myothervar");
like image 22
Jeremy Smyth Avatar answered Mar 02 '26 08:03

Jeremy Smyth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!