473,320 Members | 1,936 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,320 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 25716
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.