Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

System.getenv()

Thread view: 
Benjamin Lerman - 09 May 2006 17:13 GMT
Hi,

I'd like to know if there is a way to write an equivalent function as
System.getenv() in Java 1.4. I don't seem to be able to find a method
that returns all the environment variables, be it their names or a map
with names and values...

Thanks.

    Benjamin Lerman
Rhino - 09 May 2006 17:35 GMT
> Hi,
>
> I'd like to know if there is a way to write an equivalent function as
> System.getenv() in Java 1.4. I don't seem to be able to find a method
> that returns all the environment variables, be it their names or a map
> with names and values...

I use this static method in one of my utility classes whenever I want to
display all the environment variables. You can adapt it to do what you want.
I'm using Java 1.5 but I'm pretty sure this should work in Java 1.4 if you
modify the line which instantiates the 'set' to this, i.e. delete the angle
brackets and what is within them:

       Set set = new TreeSet();

The code displays all of the properties in alphabetical order by key name.

---------------------------------------------------------------------------------------------------------------------
   static public void displaySystemProperties() {

       /*
        * Get a collection of all of the property names in the current JVM.
Use
        * a TreeSet so that the list of names will be in alphabetical
order.
        */
       Properties sysProps = System.getProperties();
       Set<String> set = new TreeSet<String>();
       for (Enumeration enum0 = sysProps.propertyNames();
enum0.hasMoreElements();) {
           set.add((String) enum0.nextElement());
       }

       /* Display each system property's key and value. */
       System.out.println("\n  System Properties: ");
       for (Iterator it = set.iterator(); it.hasNext();) {
           String key = (String) it.next(); //determine the key name
           /* Display the key name and its value. */
           System.out.println("    " + key + " = " +
sysProps.getProperty(key));
       }

       return;
   }

---------------------------------------------------------------------------------------------------------------------

--
Rhino
Benjamin Lerman - 09 May 2006 18:18 GMT
> I use this static method in one of my utility classes whenever I want to
> display all the environment variables. You can adapt it to do what you want.
> I'm using Java 1.5 but I'm pretty sure this should work in Java 1.4 if you
> modify the line which instantiates the 'set' to this, i.e. delete the angle
> brackets and what is within them:

Thanks for your answer, but I'm not looking to the java properties, but
to the environment variables. Your code allow to get properties like
java.runtime.version, os.version, etc., I'm looking for PATH,
LD_LIBRARY_PATH etc.

Signature

    Benjamin Lerman

Rhino - 09 May 2006 18:42 GMT
>> I use this static method in one of my utility classes whenever I want to
>> display all the environment variables. You can adapt it to do what you
[quoted text clipped - 9 lines]
> java.runtime.version, os.version, etc., I'm looking for PATH,
> LD_LIBRARY_PATH etc.

Sorry, my mistake, I misread your question!

For what it's worth, there is an interesting discussion of this question in
the Google archives for this newsgroup. If you do a Google newsgroup search
on "environment variable", it should be the first hit you get; the Subject
of the thread is "Environment variable" and it was started by Stefan Poehn
on Tues Nov 16, 2004 at 11:36 am. It explains why getenv() was deprecated,
then un-deprecated and why it may be a bad idea to use it in any case.

Some of the other threads that come up in response to that search may also
be useful to you.

I know this isn't a direct answer to your original question but i don't want
to spend the next hour reading umpteen threads of information and then
trying to paraphrase it all; it makes a lot more sense if you read the
information for yourself since you know best what you are trying to do and
what your environment is.

--
Rhino
Benjamin Lerman - 09 May 2006 21:29 GMT
> I know this isn't a direct answer to your original question but i don't want
> to spend the next hour reading umpteen threads of information and then
> trying to paraphrase it all; it makes a lot more sense if you read the
> information for yourself since you know best what you are trying to do and
> what your environment is.

No problem with that, unfortunately this thread and the following do
not give me any answers. My problem is not to get the value of a
particular environment variable, but all the values of all environment
variables.

But I might be heading in the wrong direction.

My problem is: I need to exec an external process, and I need to add an
environment variable before doing so.

The problem is that if I use Runtime.exec(String), my environment is
the old one without my variable, and If I put an array with my new
environment variable, I lost my old environment.

I do not find a way, in Java 1.4, to launch an external command with a
enriched environment.

I can do it in Java 5 thanks to the System.getenv() method and that's
why I asked how to replace this method in Java 1.4, but if this is not
possible, is their another way to solve my problem?

BTW, I do not want to use JNI, because I do want to be cross-platform.

Thanks.

Signature

    Benjamin Lerman

Rhino - 10 May 2006 00:17 GMT
>> I know this isn't a direct answer to your original question but i don't
>> want
[quoted text clipped - 26 lines]
>
> BTW, I do not want to use JNI, because I do want to be cross-platform.

I'm not sure what to suggest other than what others have suggested in this
thread and the older ones I cited.

Sorry,

--
Rhino
Chris Uppal - 10 May 2006 09:24 GMT
>  My problem is: I need to exec an external process, and I need to add an
> environment variable before doing so.

This is an /inherently/ system-dependent operation.  So you can forget about
cross-platform portability (the only reason it can be "portable" in 1.5 is that
the JVM/library itself contains platform-specific code).

The question is not /whether/ you are going to be able to use cross-platform
code, but /what/ platform-specific code you will have to write.  So it's your
choice...

   -- chris
Gordon Beaton - 10 May 2006 13:25 GMT
> My problem is not to get the value of a particular environment
> variable, but all the values of all environment variables.

This can be done by running /usr/bin/printenv or similar external
program with Runtime.exec(), and parsing the values from the
InputStream. One convenient way is with Properties.load():

  Properties props = new Properties();
  Process p = Runtime.getRuntime().exec("/usr/bin/printenv");
  props.load(p.getInputStream);

> My problem is: I need to exec an external process, and I need to add
> an environment variable before doing so.

When you run a command in a Unix shell you can modify the environment
locally for that process, for example by adding variable assignments
to the command line:

 bash$ FOO=baz /usr/bin/printenv FOO
 baz
 bash$ FOO=gurka /usr/bin/printenv FOO
 gurka
 bash$ /usr/bin/printenv FOO
 (nothing)
 bash$

You can use the same technique when you invoke Runtime.exec():

 String foo = "someValue";
 String[] cmd = {
   "/bin/sh",
   "-c",
   "FOO=" + foo + " /some/path/myCommand"
 }

 Process p = Runtime.getRuntime().exec(cmd);

(etc)

Another way is to wrap the external program in a shell script or batch
file that expects the extra environment variables on the command line,
and can set them before invoking the program itself.

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

Juha Laiho - 16 May 2006 20:35 GMT
Benjamin Lerman <news+news-free@ambre.net> said:
> My problem is: I need to exec an external process, and I need to add an
>environment variable before doing so.
>
> The problem is that if I use Runtime.exec(String), my environment is
>the old one without my variable, and If I put an array with my new
>environment variable, I lost my old environment.

If you're working with just Unix platforms, you could use /usr/bin/env
as a helper to start the external application, like:

/usr/bin/env NEWVARIABLE=value /path/to/extcomm arg1 arg2

Signature

Wolf  a.k.a.  Juha Laiho     Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
        PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)

Chris Uppal - 09 May 2006 18:50 GMT
>  I'd like to know if there is a way to write an equivalent function as
> System.getenv() in Java 1.4.

There isn't one.  That, after all, is why it was [re]added in 1.5 ;-)

The nearest you can easily get is to execute a system-dependent sub-process
which writes its environment to its stdout in a parsable form, which your Java
code then parses (remembering to take proper account of embedded newlines,
spaces, quotes, etc).  Of course, that depends on the environment of the parent
Java process being reproduced in the child.  Typically that will be the case --
more or less, and depending on the system, and on how you do it.

Alternatively, you could use JNI :-(

   -- chris
Thomas Fritsch - 09 May 2006 19:00 GMT
Benjamin Lerman schrieb:
>  I'd like to know if there is a way to write an equivalent function as
> System.getenv() in Java 1.4. I don't seem to be able to find a method
> that returns all the environment variables, be it their names or a map
> with names and values...
The only way I see is to implement it in C and flange it to Java by JNI.

//the Java side:
  public class MySystem {
    public static native String getenv(String name);
  }

together with your C-implementation of the method above, where you call
the C-function getenv (as defined in <stdlib.h>).

Signature

"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.