Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Use java to set and get environment variable ?

Posted on 2002-06-21
8
575 Views
Last Modified: 2007-12-19
How do I use java to set and get environment variable ?
0
Comment
Question by:j_script
8 Comments
 

Expert Comment

by:javaXpert
ID: 7100325
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
 

Author Comment

by:j_script
ID: 7100889
Can I use pure java to get/set the environment variable ?
I cannot use JNI.
0
 

Expert Comment

by:javaXpert
ID: 7100915
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Accepted Solution

by:
fi76631 earned 100 total points
ID: 7101156
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
 
LVL 92

Expert Comment

by:objects
ID: 7101185
You can get them by passing the value via the command line.
0
 
LVL 1

Expert Comment

by:mraible
ID: 7103531
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
 
LVL 9

Expert Comment

by:doronb
ID: 7103553
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
 
LVL 5

Expert Comment

by:Netminder
ID: 8581058
Force-accepted.

Netminder
EE Admin
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question