getLoginName() getHomeDir() getHostName()

S

shea martin

getLoginName() getHomeDir() getHostName()
Are there any functions in the Java API that acomplish the above? I
know I could probably use System.getenv(), USER/LOGNAME, HOME, HOSTNAME,
but these variables are not always reliable, escpecially on windows
where they only exist if the user sets them.

I have only recently returned to the world of java (after a 4 year LOA),
but in the meantime I have been using Qt. Qt is x-platform and provides
these functions, so I am sure that java does as well. But my googles
kept turning up JSP and Jscript stuff.

Thanks,

~S
 
J

jungi

shea said:
getLoginName() getHomeDir() getHostName()
Are there any functions in the Java API that acomplish the above? I
know I could probably use System.getenv(), USER/LOGNAME, HOME, HOSTNAME,
but these variables are not always reliable, escpecially on windows
where they only exist if the user sets them.

I have only recently returned to the world of java (after a 4 year LOA),
but in the meantime I have been using Qt. Qt is x-platform and provides
these functions, so I am sure that java does as well. But my googles
kept turning up JSP and Jscript stuff.

Thanks,

~S

System.getProperties().store(System.out, "");

--list of current properties,
find out there keys which you want (user.home,...) and get it via
System.getProperty(propertyName);

HTH

--jungi
 
P

Paul Lutus

shea said:
getLoginName() getHomeDir() getHostName()
Are there any functions in the Java API that acomplish the above? I
know I could probably use System.getenv(), USER/LOGNAME, HOME, HOSTNAME,
but these variables are not always reliable, escpecially on windows
where they only exist if the user sets them.

Not true. Various versions of Windows treat these values differently, but it
has little to do with the user's choices.

Just look at the output of System.getProperties() in each relevant platform
and adjust your code as required to accommodate the differences.

import java.util.*;

public class SystemProperties {

static String tab(String s,int t)
{
StringBuffer sb = new StringBuffer(s);
while (sb.length() < t) {
sb.append(" ");
}
return sb.toString();
}
public static void main(String[] args) {
Properties p = System.getProperties();
for (Enumeration e = p.propertyNames() ; e.hasMoreElements() ;) {
String key = (String) e.nextElement();
System.out.println(tab(key,36) + " = " + p.getProperty(key));

}
}
};
 
S

shea martin

jungi said:
System.getProperties().store(System.out, "");

--list of current properties,
find out there keys which you want (user.home,...) and get it via
System.getProperty(propertyName);

HTH

--jungi
Sweet, that is better than sifting through man pages searching for what
I want.

I also found:
String hname = InetAddress.getLocalHost().getHostName();

~S
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top