• Post Reply Bookmark Topic Watch Topic
  • New Topic

environment variables

 
Kalpesh Soni
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i wanted to access some environment variables from my java program
i searched the system class
but it seems that only those variables which already have key can be shown
can somebody help me ??
------------------
KS
 
Brett Knapik
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you might have to use JNI.
------------------
I wish there was a button on my monitor to turn up the intellegince.
Theres a button called 'brightness' but it doesn't work
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The usual solution to this is to start your Java program with a script which passes in the values you need on the command-line using "-Dname=value". The values are then available from System.getProperty.
for unix, linux etc.:
java -DUSERNAME="$USERNAME" -DPASSWORD="$PASSWORD" MyClass
for DOS, Windows etc.:
java -DUSERNAME=%USERNAME% -DPASSWORD=%PASSWORD% MyClass
 
Matt O'Keefe
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code will give you access to all environment variables... just parse the results for the variable that you're looking for...
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception {
Process p = Runtime.getRuntime().exec("env"); // exec "set" for Win NT/2000
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = reader.readLine();
while (str != null) {
System.out.println(str);
str = reader.readLine();
}
}
}
 
Skip Cole
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just wanted to say thanks!
This helped us write a utility to look at our classpaths.
Skip
 
John Wetherbie
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also pass in the name of a file to your program and merge the properties in the file with the properties your system is using. If you have the Java in a Nutshell book there is a chapter on this. I've used this approach on a variety of programs.
John
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!