Use java to set and get environment variable ?

How do I use java to set and get environment variable ?
j_scriptAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
fi76631Connect With a Mentor Commented:
Reading an environment variable is relatively simple:

String envVar = System.getProperty("PROPERTY_NAME");

However, I am guessing that you are more interested in setting rather than getting.

In short, its not possible if you run on NT, it is possible on UNIX.

The reason that its not possible on NT is that each process runs in its own process space and has no access to the process space that created it.

Therefore, if you COULD set the environment variable only YOUR process would be able to see it. No other processes would be aware of it or its value.

If all you want to do is change it in the context of the currently running process, then you could use:

String cmd = "set myEnvVar=\"Hello World\"";
Runtime.getRuntime().exec(cmd);

If you explain 'why' you need to do this in more detail, maybe I can provide you with an alternative solution.
0
 
javaXpertCommented:
Use JNI to call a native function that will handle the call to the host-specific API. On Win32, the two following functions are responsible for getting and setting environment variables:

DWORD GetEnvironmentVariable(
  LPCTSTR lpName,  // environment variable name
  LPTSTR lpBuffer, // buffer for variable value
  DWORD nSize      // size of buffer
);

BOOL SetEnvironmentVariable(
  LPCTSTR lpName,  // environment variable name
  LPCTSTR lpValue  // new value for variable
);

Please note that when using GetEnvironmentVariable, the length of the desired environment variable length is limited to the the size of the allocated buffer. It is always a good idea to enlarge the buffer if the allocated one does not suffice. More information about these functions can be found on MSDN.
0
 
j_scriptAuthor Commented:
Can I use pure java to get/set the environment variable ?
I cannot use JNI.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
javaXpertCommented:
No. Accessing and modifing environment variables cannot be done in Pure Java, though you can avoid using JNI. Anyway, why can't you use JNI?
0
 
objectsCommented:
You can get them by passing the value via the command line.
0
 
mraibleCommented:
I set a "dateFormat" property in a StartupServlet in my web application with:

System.setProperty("dateFormat", "MM/dd/yyyy");

For simplicity - I've replaced my variables with the string values you see above.

This property is then available for all my servlets/classes used throughout the lifecycle of my web application using:

System.getProperty("dateFormat");

HTH,

Matt
0
 
doronbCommented:
Hi,


I suppose it might be silly of me to ask, but why not read/write these properties to a properties file which all your Servlets can access?


Doron :o
0
 
NetminderCommented:
Force-accepted.

Netminder
EE Admin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.