471,852 Members | 836 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,852 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 25396
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

Post your reply

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

Similar topics

28 posts views Thread by Christian | last post: by
13 posts views Thread by Jimmy Cracker | last post: by
1 post views Thread by Bonj | last post: by
4 posts views Thread by | last post: by
NeoPa
reply views Thread by NeoPa | last post: by
aboka
reply views Thread by aboka | last post: by

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.