Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to read the "HOST" environment variable

803 views
Skip to first unread message

Daniel

unread,
Jan 22, 2001, 9:11:08 PM1/22/01
to
Hello,
I want to be able to get the value of the HOST environment variable (I'm
running java on unix). I know about System.getProperty(string), but
there's no pre-defined property that java has defined for the HOST
property. Before they deprecated it, I would have called
System.getenv("HOST"). So how do I do this?

Thanks,
Daniel

--
To reply to me directly, please remove "ANTISPAM" from the reply-to
email address.


Chris Smith

unread,
Jan 22, 2001, 10:22:53 PM1/22/01
to
"Daniel" <dlrubinA...@yahoo.com> wrote ...

> I want to be able to get the value of the HOST environment variable (I'm
> running java on unix). I know about System.getProperty(string), but
> there's no pre-defined property that java has defined for the HOST
> property. Before they deprecated it, I would have called
> System.getenv("HOST"). So how do I do this?

Try java.net.InetAddress.getLocalHost().getHostName() -- perhaps that's what
you want. The problem with using HOST as an environment variable is that it
doesn't exist on all systems.

Chris Smith

Daniel

unread,
Jan 22, 2001, 11:03:09 PM1/22/01
to
This call returns null. Am I supposed to do something else before calling it?

Thanks.

Chris Smith wrote:

--

Nils O. Selåsdal

unread,
Jan 23, 2001, 3:27:36 AM1/23/01
to

"Daniel" <dlrubinA...@yahoo.com> wrote in message
news:3A6D027D...@yahoo.com...

> This call returns null. Am I supposed to do something else before calling
it?
As do my Linux (echo $HOST) , Play around with the InetAddress methods.....

Harald Bock

unread,
Jan 23, 2001, 5:13:08 AM1/23/01
to
> I want to be able to get the value of the HOST environment variable (I'm
> running java on unix). I know about System.getProperty(string), but
> there's no pre-defined property that java has defined for the HOST
> property. Before they deprecated it, I would have called
> System.getenv("HOST"). So how do I do this?

two possibilities
a) use the InetAddress interface (preferred)
b) use System.getProperty("env.hostname")
while starting the VM with additional option -Denv.hostname="`hostname`"
This also works in principle on all platforms but you have to modify
the call of `hostname` to the platform needs...


--
Harald Bock, Lucent Technologies, Germany
Phone : +49 911 526-3729 Dept. :FLI3 (GSM Development Software Support)
Mobile: +49 170 935-8003 Addr. :Thurn-und-Taxisstr.14, 90411 Nuremberg
Fax : +49 911 526-3183 mailto:HB...@Lucent.com

Daniel

unread,
Jan 23, 2001, 11:09:16 PM1/23/01
to
My java app is a java servlet, and I'm not aware of a way to use the -D option
as you describe--is there a way for my code to get the environment variable
running as a servlet?

Thanks,
Daniel

Harald Bock wrote:

--

Gerard Oberle

unread,
Jan 25, 2001, 9:51:26 PM1/25/01
to
When the Servlet is initialized, it will be passed (through the Servlet engine)
zero or more initialization parameters. These may be retrieved, by name, using
the ServletConfig's getInitParameter(String) method. Of course, you may not be
able to use `echo $HOST`. For example, I cannot do this with WebSphere.

You might also try using Runtime.exec(...) and issuing a hostname command.

Personally, I think it was stupid to deprecate System.getenv(String). I have yet
to work on an operating system without some environment-variable-like metaphor.
And if Macs don't have such a thing, then have an empty environment variable
space. Besides, they didn't deprecate Runtime.exec(String command, String[]
envp); I wonder what that does on toy operating systems? But what do I know?

Hope this helps!
-Jerry Oberle

0 new messages