Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getBoolean in Java.lang

Status
Not open for further replies.

munnanext

Programmer
Aug 20, 2004
49
US
public class getBooleanDemo {

public static void main(String args[]){
boolean b;
String propertyName ="true";
b = Boolean.getBoolean(propertyName);
System.out.println(b);
}

}

Can you pl explain me what getBoolean does ?
I am reading this in the Book and it says propertyName should be system property. I am not clear.

Thanks for solution
 
A system-property is something special.
Your propertyName has nothing to do with it - it's just a variable-name you use.

java.lang.System.getProperty (String key);

returns a value for a key.
Imagine System-Properties as key-value-pairs:
Code:
HOME=/home/munnanext
EDITOR=mcedit
LINES=25
COLUMNS=80
HUSHLOGIN=FALSE
now you may ask:
Code:
String hl = System.getProperty ("HUSHLOGIN");
boolean b = Boolean.getBoolean (bhl);


seeking a job as java-programmer in Berlin:
 
I really Appreciate your Reply. Thanks for the clarification.
 
based on your reply I have written a program like below to read the windir (which is a system variable) in my system. but when I run the program it is return me null instead of C:\WINNT.

Any help ? Thanks

public class getSystemProperty {

public static void main(String args[]){
String h1;
//String propertyName ="true";
h1 = System.getProperty("windir");

System.out.println(h1);
}

}
 
You cannot do it like that.

System.getProprerty() only works for variables passed into the JVM using the "-D" switch.
EG :

java -Dwindir=something MyProg

You can however do it like this :

Code:
import java.io.*;
public class Test {
 	public static void main(String args[]) throws Exception {

		Process p = Runtime.getRuntime().exec("cmd /c set WINDIR");
		p.waitFor();
		BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
		String envVar = br.readLine();
		br.close();

		System.out.println(envVar);
	}
}

--------------------------------------------------
Free Database Connection Pooling Software
 
I am intrested more on the first option. Sorry I couldn't able to follow you completely.

I am running the program like this
---------------------------------------
java -Dwindir=something readSystemVariable

I got hte following output
---------------------------------------
This is from the application something

My Source is
---------------------------------------

public class readSystemVariable{

public static void main(String args[]){
try{
String h1 = new String(System.getProperty ("windir"));
System.out.println("This is from the application "+ h1);
}catch( Exception e)
{
e.toString();
}
}

}

my Expected output is
------------------------
C:\winnt (Which is in my System variable value)

Apprecite your help
 
try :

java -Dwindir=%WINDIR% MyProg

If that doesn't work, then you will have to use the method I suggested before.

--------------------------------------------------
Free Database Connection Pooling Software
 
For some properties you may use:
Code:
			System.out.println ("-----------------------Properties: ---------------");
		Properties p = System.getProperties ();
		p.list (System.out);
		System.out.println ("-----------------------and here env:--------------");
		Map <String, String> m = System.getenv ();
		Set<Map.Entry<String, String>> s = m.entrySet ();
		for (Map.Entry me : s)
		{
			System.out.println (me.getKey () + " = " + me.getValue ());
		}
Properties will probably not contain windir, but I bet you find your %WINDIR% with getenv().
Here a few values I found:
Code:
-----------------------Properties: ---------
-- listing properties --
java.vm.version=1.5.0-beta3-b57
path.separator=:
user.country=DE
user.dir=/home/stefan/proj/mini/forum
java.runtime.version=1.5.0-beta3-b57
os.arch=i386
java.io.tmpdir=/tmp
os.name=Linux
sun.jnu.encoding=ISO-8859-1
os.version=2.6.8.1
file.separator=/
sun.cpu.endian=little
-----------------------and here env:--------------
ANT_HOME = /usr/local/lib/ant
LANG = de_DE
PGDATA = /home/postgres/data
HZ = 100
_XKB_CHARSET = de-latin1-nodeadkeys
USER = stefan
SHELL = /bin/bash
QTDIR = /usr/lib/qt
remark: The code is 1.5.0-beta and might not work with 1.4.2.

Visit the docs for java.lang.System and find out, how to do it with java 1.4.x.


seeking a job as java-programmer in Berlin:
 
Bear in mind that JVM System properties are completely different to environment properties - and the only way to get OS environment properties are as I demonstrated before.



--------------------------------------------------
Free Database Connection Pooling Software
 
sedj, while you're mostly right, the javadocs say:
java.lang.System.getenv:
getenv

public static Map<String,String> getenv()

Returns an unmodifiable string map view of the current system environment. The environment is a system-dependent mapping from names to values which is passed from parent to child processes.
And running a diff on the modified code above:
Code:
for (Map.Entry me : s)
{
    System.out.println (me.getKey () +"="+ me.getValue ());
}
and a plain 'env' on the commandline (plus sort in both cases and removing the Properties-part) shows 35 identical lines, 2 different and 2 additional for the java-getenv().

One of the differences is the invoking command - to no surprise, being /usr/bin/env in the one case and /opt/java/bin/java in the second.
The second is the LD_LIBRARY_PATH which is longer for the java-version, some java-dirs added.

The two additional java-entries are:
Code:
< NLSPATH=/usr/dt/lib/nls/msg/%L/%N.cat
< XFILESEARCHPATH=/usr/dt/app-defaults/%L/Dt
Unfortunately /usr/dt doesn't exist, so I have a broken java-installation?
And %L %N - do they mean language, nation? de DE in my case?

What's wrong with 'getenv ()'?
Ah - I see: new since 1.5

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top