tags:

views:

369

answers:

4

How can I obtain the physical machine name that my jvm is running in?

(Physical = OS, up to vmware...)

A: 

Take a look at the System class, the one used to do System.out.println, etc...

Martin Dale Lyness
+6  A: 
String computername=InetAddress.getLocalHost().getHostName();
System.out.println(computername);
jsight
+2  A: 

I'm not exactly sure what you mean by Physical Machine Name. Your comment "(Physical = OS, up to vmware...)" needs explaining to me.

But you can use System.getProperty(String key) where key is one of the keys found here: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#getProperties()

That should tell you OS name. If you need hostname use jsight's advice.

jbu
I mean the name of the computer the JVM is running in.Most likely a physical computer, but if the JVM is running inside another virtual machine then that name is good.
ripper234
+1  A: 

Couple options, since I'm not sure what you want:

RuntimeMXBean rmx = ManagementFactory.getRunTimeMXBean();
System.out.println(rmx.getName());

Or...

System.out.println(InetAddress.getLocalHost().getHostName());

Or on Linux

Runtime r = Runtime.getRuntime();
Process p = r.exec("uname -a");
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream());
System.out.println(r.readLine());
Gandalf