473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Set an environment Variable in Java.

dmjpro
2,476 2GB
There is an API to get the environment variable, "System.getEnv".
But there is no setEnv method ?
Was there any problem to implement that?
Please explain ???

Debasis Jana.
Mar 14 '08 #1
10 25740
JosAH
11,448 Expert 8TB
There is an API to get the environment variable, "System.getEnv".
But there is no setEnv method ?
Was there any problem to implement that?
Please explain ???

Debasis Jana.
You don't need it; if you want a changeable environment you can build your own:

Expand|Select|Wrap|Line Numbers
  1. public class MyEnvironment extends HashMap<String, String> {
  2.  
  3.    // singleton
  4.    private MyEnvironment env;
  5.    public synchronized MyEnvironment getInstance() {
  6.       if (env == null)
  7.          env= new MyEnvironment(System.getEnv());
  8.       return env;
  9.    }
  10.  
  11.    private MyEnvironment(Map<String, String> copy) {
  12.       super(copy);
  13.    }
  14. }
  15.  
kind regards,

Jos
Mar 14 '08 #2
dmjpro
2,476 2GB
You don't need it; if you want a changeable environment you can build your own:

Expand|Select|Wrap|Line Numbers
  1. public class MyEnvironment extends HashMap<String, String> {
  2.  
  3.    // singleton
  4.    private MyEnvironment env;
  5.    public synchronized MyEnvironment getInstance() {
  6.       if (env == null)
  7.          env= new MyEnvironment(System.getEnv());
  8.       return env;
  9.    }
  10.  
  11.    private MyEnvironment(Map<String, String> copy) {
  12.       super(copy);
  13.    }
  14. }
  15.  
kind regards,

Jos

Thanks for your reply ....!
But how could I make it persistent ???
Should I use Serialization?

Anyway one more thing ..why SUN failed to design System.setEnv?
I need this explain ..please!

Debasis Jana
Mar 14 '08 #3
JosAH
11,448 Expert 8TB
Thanks for your reply ....!
But how could I make it persistent ???
Should I use Serialization?

Anyway one more thing ..why SUN failed to design System.setEnv?
I need this explain ..please!

Debasis Jana
Sun didn't fail in designing that method because, as I wrote before, you don't
need it; have a look at the Runtime.exec( ... ) methods: you can pass it
an environment yourself so you don't have to pass it your own environment. It
simply isn't needed to alter your own environment.

kind regards,

Jos
Mar 14 '08 #4
dmjpro
2,476 2GB
Sun didn't fail in designing that method because, as I wrote before, you don't
need it; have a look at the Runtime.exec( ... ) methods: you can pass it
an environment yourself so you don't have to pass it your own environment. It
simply isn't needed to alter your own environment.

kind regards,

Jos

I came to know that ..but it is platform dependent ..
That's why I asked.

Anyway thanks for your help.
But how could I make that persistent ...because after JVM shuts down then how could I get my own Environment?

Debasis Jana
Mar 14 '08 #5
JosAH
11,448 Expert 8TB
I came to know that ..but it is platform dependent ..
That's why I asked.

Anyway thanks for your help.
But how could I make that persistent ...because after JVM shuts down then how could I get my own Environment?

Debasis Jana
That's not how environments work: when a process terminates its environment
is gone as well.

kind regards,

Jos
Mar 14 '08 #6
dmjpro
2,476 2GB
That's not how environments work: when a process terminates its environment
is gone as well.

kind regards,

Jos

Then how these work????
JAVA_HOME ..... something something ..... these set always ..
how??

Debasis Jana.
Mar 14 '08 #7
JosAH
11,448 Expert 8TB
Then how these work????
JAVA_HOME ..... something something ..... these set always ..
how??

Debasis Jana.
So you have forgotten that it's you who set that variable one day in the past?
It didn't end up in your environment automagically.

And if you're innocent some other process passed it on to its child process,
just like you did in your Runtime.exec() call.

kind regards,

Jos
Mar 14 '08 #8
dmjpro
2,476 2GB
So you have forgotten that it's you who set that variable one day in the past?
It didn't end up in your environment automagically.

And if you're innocent some other process passed it on to its child process,
just like you did in your Runtime.exec() call.

kind regards,

Jos
Yeah GOT man ..thanks a lot again!

Debasis Jana!
Mar 14 '08 #9
Hi iam facing a similar problem but i just need to set the User when executing a Process and i dont need to save the enviroment could someone help me with this
May 15 '10 #10
jkmyoung
2,057 Expert 2GB
Do you mean windows user? Is this like "Run As Administrator?" Start your own thread for your own question.
May 17 '10 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Albretch | last post by:
that was set up for and/or by this user in the OS. Say, you set WEB_DIR as an environment variable pointing to certain folder or root directory Could you go like this <?xml version='1.0'...
28
by: Christian | last post by:
Another question from a not even newbie: In Unix you can set an environment variable with the command export PYTHONPATH but I would like to set the variable from at .py script. So my question...
13
by: Jimmy Cracker | last post by:
Is it completely impossible in UNIX to push an environment variable to the parent shell? I would like to do something like this: main(int argc, char *argv) { char *var; var = (char...
1
by: Bonj | last post by:
Hi My application installs a front-end GUI, which runs code when buttons are clicked. It also installs a command-line utility, that is a console application. They are both installed to the...
4
by: | last post by:
Hi all, I am trying to append a certain string to the PATH environment variable programmatically. I am able to read what is in the variable using the System.Environment method...
6
by: yaron | last post by:
Hi, my application use environment variable, let call it FOO. how can i add the FOO environment variable to my project or my solution, so the line string foo =...
0
by: Joe HM | last post by:
Hello - I am putting together a little ConsoleApplication that is supposed to check for an Environment Variable and create it if it does not exist. I found some code that will add a new...
4
by: rAinDeEr | last post by:
Hi, We have a Test Database in Linux with DB2 v8.2. DB2 was installed in /db2home/db2inst1. Some one moved the all folder db2inst1 to Trash. I moved it back to db2home/db2inst1 But now,...
4
by: vkbishnoi | last post by:
I need to create an environment variable for another local user in windows. For example, say suppose there are 2 users in the system (User1 and User2). Currently User1 is logged in the system. Now Is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.