Solved

Read an enviroment variable from a java program

Posted on 2001-06-10
8
694 Views
Last Modified: 2008-02-01
I want to get an enviroment variable from a java program.
Can you help me?
Thanks!
0
Comment
Question by:rzvika
  • 4
  • 3
8 Comments
 
LVL 3

Expert Comment

by:SEve
ID: 6172950
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.


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

seve
0
 

Author Comment

by:rzvika
ID: 6172968
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)
0
 
LVL 3

Expert Comment

by:SEve
ID: 6172975
i really don't sure about most of evnironment variables, because of system dependency... maybe native methods can help

seve
0
 
LVL 2

Accepted Solution

by:
Andrey_Kulik earned 100 total points
ID: 6173061
Hi

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
    break;
}

prop.list(System.out);

Best regards
Andrey
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:rzvika
ID: 6173137
Will be it good for unix too ?
0
 
LVL 3

Expert Comment

by:SEve
ID: 6173144
it won't be good for unix because of "cmd /C set", which is windows nt/2000 command. for unix run just "set".

seve
0
 

Author Comment

by:rzvika
ID: 6177282
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)
0
 

Author Comment

by:rzvika
ID: 6186217
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 ?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java and GPO 11 66
Problem to start Neon 20 83
HSSFWorkbook cannot be resolved error 10 49
Oracle SQL syntax check  without executing 6 29
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

929 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now