• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 713
  • Last Modified:

Read an enviroment variable from a java program

I want to get an enviroment variable from a java program.
Can you help me?
  • 4
  • 3
1 Solution
public static Properties java.lang.System.getProperties()

Determines the current system properties.
If there is a security manager, its checkPropertiesAccess method is called with no arguments. This may result in a security exception.

The current set of system properties is returned as a Properties object. If there is no current set of system properties, a set of system properties is first created and initialized.

This set of system properties always includes values for the following keys: Key Description of Associated Value
java.version Java version number
java.vendor Java vendor-specific string
java.vendor.url Java vendor URL
java.home Java installation directory
java.class.version Java class format version number
java.class.path Java class path
os.name Operating system name
os.arch Operating system architecture
os.version Operating system version
file.separator File separator ("/" on UNIX)
path.separator Path separator (":" on UNIX)
line.separator Line separator ("\n" on UNIX)
user.name User's account name
user.home User's home directory
user.dir User's current working directory

public static String java.lang.System.getProperty(String key)

Gets the system property indicated by the specified key.
First, if there is a security manager, its checkPropertyAccess method is called with the key as its argument. This may result in a system exception.

If there is no current set of system properties, a set of system properties is first created and initialized in the same manner as for the getProperties method.

key - the name of the system property.
the string value of the system property, or null if there is no property with that key.

rzvikaAuthor Commented:
It doesn't know even the PATH variable.
I think it contains a small amount of properties related to java (but it definitely doesn't contain others)
i really don't sure about most of evnironment variables, because of system dependency... maybe native methods can help

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!


i hope this helps

java.util.Properties prop = new java.util.Properties();
java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(Runtime.getRuntime().exec("cmd /C set").getInputStream()));
while (true)
  String str = in.readLine();
  if (str != null)
     int index = str.indexOf("=");
     if (index != -1)
       prop.put(str.substring(0, index), str.substring(index+1));
  } else


Best regards
rzvikaAuthor Commented:
Will be it good for unix too ?
it won't be good for unix because of "cmd /C set", which is windows nt/2000 command. for unix run just "set".

rzvikaAuthor Commented:
1. The command for Sun OS is: "command set"
2. When I try in Win98 "command /C set" it thrwos me this:
java.io.IOException: CreateProcess: command /C set  error=0    
at java.lang.Win32Process.create(Native Method)    
at java.lang.Win32Process.<init>(Unknown Source)    
at java.lang.Runtime.execInternal(Native Method)    
at java.lang.Runtime.exec(Unknown Source)    
at java.lang.Runtime.exec(Unknown Source)    
at java.lang.Runtime.exec(Unknown Source)    
at avaya.util.EnvironmentVariables.init(EnvironmentVariables.java:51)    
at avaya.util.EnvironmentVariablesApplet.init(EnvironmentVariablesApplet.java:34)    
at sun.applet.AppletPanel.run(Unknown Source)    
at java.lang.Thread.run(Unknown Source)
rzvikaAuthor Commented:
I've succeeded, the command is:
"command.com /C set"

...but I do have a proplem:
The console window is opened and I don't want it.
Is there a way to open it minimized ?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now