System.getenv()

  • Thread starter Benjamin Lerman
  • Start date
B

Benjamin Lerman

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
 
R

Rhino

Benjamin Lerman said:
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;
}
 
B

Benjamin Lerman

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.
 
R

Rhino

Benjamin Lerman said:
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.
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.
 
C

Chris Uppal

Benjamin said:
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
 
T

Thomas Fritsch

Benjamin said:
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>).
 
B

Benjamin Lerman

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.
 
R

Rhino

Benjamin Lerman said:
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.

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

Sorry,
 
C

Chris Uppal

Benjamin said:
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
 
G

Gordon Beaton

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
 
J

Juha Laiho

Benjamin Lerman 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
 
Joined
Mar 27, 2009
Messages
6
Reaction score
0
sasami009.spaces.live.com/blog/cns!21E909526CBB58A2!113.entry 延长离心泵使用周期的有效途径 sasami009.spaces.live.com/blog/cns!21E909526CBB58A2!112.entry 隔膜泵与柱塞泵性能比较 sasami009.spaces.live.com/blog/cns!21E909526CBB58A2!111.entry 消防泵的选型 sasami009.spaces.live.com/blog/cns!21E909526CBB58A2!110.entry 螺杆泵在污水处理过程中的应用 sasami009.spaces.live.com/blog/cns!21E909526CBB58A2!109.entry 水泵检验标准 引言 </p>
<p>/sasami009/blog/item/b6c7dc453644678ab2b7dcf0.html 针型阀概述/sasami009/blog/item/17cb522f0857a5584fc226f7.html 阀门杆开关不灵的原因/sasami009/blog/item/e7466d39c513c3fab211c710.html 阀门营销实战 阀门营销实战/sasami009/blog/item/f423cb01e37cd580e950cdd7.html 液下泵的工作原理/
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top