Set an environment Variable in Java.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    Set an environment Variable in Java.

    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.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by dmjpro
    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:

    [code=java]
    public class MyEnvironment extends HashMap<String, String> {

    // singleton
    private MyEnvironment env;
    public synchronized MyEnvironment getInstance() {
    if (env == null)
    env= new MyEnvironment(S ystem.getEnv()) ;
    return env;
    }

    private MyEnvironment(M ap<String, String> copy) {
    super(copy);
    }
    }
    [/code]

    kind regards,

    Jos

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by JosAH
      You don't need it; if you want a changeable environment you can build your own:

      [code=java]
      public class MyEnvironment extends HashMap<String, String> {

      // singleton
      private MyEnvironment env;
      public synchronized MyEnvironment getInstance() {
      if (env == null)
      env= new MyEnvironment(S ystem.getEnv()) ;
      return env;
      }

      private MyEnvironment(M ap<String, String> copy) {
      super(copy);
      }
      }
      [/code]

      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

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by dmjpro
        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

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          Originally posted by JosAH
          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

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by dmjpro
            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

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              Originally posted by JosAH
              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.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by dmjpro
                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

                Comment

                • dmjpro
                  Top Contributor
                  • Jan 2007
                  • 2476

                  #9
                  Originally posted by JosAH
                  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!

                  Comment

                  • jl880703
                    New Member
                    • May 2010
                    • 3

                    #10
                    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

                    Comment

                    • jkmyoung
                      Recognized Expert Top Contributor
                      • Mar 2006
                      • 2057

                      #11
                      Do you mean windows user? Is this like "Run As Administrator?" Start your own thread for your own question.

                      Comment

                      Working...