The Artima Developer Community
Sponsored Link

Java Answers Forum
Computer Name using Java

3 replies on 1 page. Most recent reply: Mar 2, 2002 7:15 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Raj

Posts: 4
Nickname: raj
Registered: Feb, 2002

Computer Name using Java Posted: Feb 28, 2002 5:26 PM
Reply to this message Reply
Advertisement
I would like to find a way in which i can get hold of the computer name using java.


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Computer Name using Java Posted: Mar 1, 2002 9:58 AM
Reply to this message Reply
To get the current system properties you could do something like the following:
Properties properties = System.getProperties();
properties.list(System.out);


COMPUTERNAME is NOT one of the Properties that you can acess from a statement such as:
System.out.println("COMPUTERNAME: "+ System.getProperty("COMPUTERNAME"));
This will return a null string.

COMPUTERNAME appears to be an operating system specific environment variable.
You can get it by doing a set command and look for the line similar to the following:
COMPUTERNAME=GGN-D9JRNX01

or set COMPUTERNAME gives you just this line

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Computer Name using Java Posted: Mar 1, 2002 10:08 AM
Reply to this message Reply
Do you mean "echo %computername%" toward the end of your post (when mentioning how to see one environment variable)?

By the way, this environment variable is not present on Windows 9x/Me systems, so I don't know what the trick would be on those...

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Computer Name using Java Posted: Mar 2, 2002 7:15 PM
Reply to this message Reply
Yeah you are correct. I was running on a Windows 2000 machine when I wrote that. The computer name environment variable is not shown on the Windows ME machine I am messing with right now.

Machine name probably only applies to Windows NT and 2000 machines.

I wonder if the original question was about finding the IP address instead of the machine name?

Something like the following would do that:

	/**	
	*/
	public String getLocalHostName(){
		String name = "";
		try{
			name =  getHostName(InetAddress.getLocalHost());
		}catch(UnknownHostException unhe){
			System.err.println("Exception: " + unhe.getMessage());		
		}catch (Exception e){
			System.err.println("Exception: " + e.getMessage());		
		}
		return name;
	}
 
	/**	
	*/
	public InetAddress getLocalHostAddress(){
		InetAddress address = null;
		try{
			address = InetAddress.getLocalHost();
		}catch(UnknownHostException unhe){
			System.err.println("Exception: " + unhe.getMessage());		
		}catch(Exception e){
			System.err.println("Exception: " + e.getMessage());		
		}
		return address;		
	}

Flat View: This topic has 3 replies on 1 page
Topic: java.io.FilePermission Previous Topic   Next Topic Topic: Query

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use