Changing JAVA_HOME in cmd temporarily doesn't change PATH temporarily in windows












1















I hava defined JAVA_HOME to jdk11 in users' environment variable and PATH contains %JAVA_HOME%/bin in it.



Now I want to switch to jdk8 temporarily in command-prompt (cmd) by changing JAVA_HOME to jdk8 but it doesn't change value in path or java -version doesn't change in current instance of cmd.



P.S.- I have deleted c:Program DataOraclejava.. files to be avoid any confusion










share|improve this question





























    1















    I hava defined JAVA_HOME to jdk11 in users' environment variable and PATH contains %JAVA_HOME%/bin in it.



    Now I want to switch to jdk8 temporarily in command-prompt (cmd) by changing JAVA_HOME to jdk8 but it doesn't change value in path or java -version doesn't change in current instance of cmd.



    P.S.- I have deleted c:Program DataOraclejava.. files to be avoid any confusion










    share|improve this question



























      1












      1








      1








      I hava defined JAVA_HOME to jdk11 in users' environment variable and PATH contains %JAVA_HOME%/bin in it.



      Now I want to switch to jdk8 temporarily in command-prompt (cmd) by changing JAVA_HOME to jdk8 but it doesn't change value in path or java -version doesn't change in current instance of cmd.



      P.S.- I have deleted c:Program DataOraclejava.. files to be avoid any confusion










      share|improve this question
















      I hava defined JAVA_HOME to jdk11 in users' environment variable and PATH contains %JAVA_HOME%/bin in it.



      Now I want to switch to jdk8 temporarily in command-prompt (cmd) by changing JAVA_HOME to jdk8 but it doesn't change value in path or java -version doesn't change in current instance of cmd.



      P.S.- I have deleted c:Program DataOraclejava.. files to be avoid any confusion







      windows command-line java path jdk






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 9:53









      DavidPostill

      105k25227262




      105k25227262










      asked Jan 3 at 9:47









      the1dererthe1derer

      82




      82






















          2 Answers
          2






          active

          oldest

          votes


















          2














          It doesn't change value in path or java -version doesn't change in current instance of cmd



          You have to restart cmd for the changes to take effect as a cmd shell inherits it environment from the parent process.





          So what is the correct way to switch between Java versions from the command line?



          Use a set of batch files, as follows:




          Being a Java developer, I always compile and test my code on different Java versions. But switching between them is a huge problem. So finally I found an easy method to do this. You have to create following batch files and place them in directory you open your command line in or in SYSTEM PATH. You can use you favorite text editor to create these files.



          jdk14.bat



          @echo off
          echo Setting JAVA_HOME
          set JAVA_HOME=C:j2sdk1.4.2_12
          echo setting PATH
          set PATH=C:j2sdk1.4.2_12bin;%PATH%
          echo Display java version
          java -version


          jdk15.bat



          @echo off
          echo Setting JAVA_HOME
          set JAVA_HOME=C:Program FilesJavajdk1.5.0_12
          echo setting PATH
          set PATH=C:Program FilesJavajdk1.5.0_12bin;%PATH%
          echo Display java version
          java -version


          jdk16.bat



          @echo off
          echo Setting JAVA_HOME
          set JAVA_HOME=C:Program FilesJavajdk1.6.0_11
          echo setting PATH
          set PATH=C:Program FilesJavajdk1.6.0_11bin;%PATH%
          echo Display java version
          java -version


          Make sure you assign the appropriate JAVA_HOME value in batch files, according to your Java installation. Whenever you want to switch between Java versions, just run the respective batch file and you are done.



          Note: JAVA_HOME and the path to java must always refer to the exact same version of the JDK. If you mix them up, unpredictable things will happen!




          Source Switch between different JDK versions in Windows | Oracle Pranav's Blog






          share|improve this answer


























          • but on restarting the temporary value of JAVA_HOME will be lost

            – the1derer
            Jan 3 at 10:00













          • You have to set it permanently and then change it back when you are done.

            – DavidPostill
            Jan 3 at 10:01











          • Answer updated.

            – DavidPostill
            Jan 3 at 10:06



















          0














          The reason for this is that the variable reference in PATH is expanded at the time of the assignment to PATH, any later changes are ignored, the reference to the original value is lost. It's like making a copy of the value of the variable, not creating a reference to the variable.



          set JAVA_HOME=C:dir1
          PATH=%JAVA_HOME%
          set JAVA_HOME=C:dir2
          PATH


          This will output C:dir1 (value of JAVA_HOME at the time of the assignment) and not %JAVA_HOME%.



          You need a script like this to call after a change to JAVA_HOME:



          PATH=%JAVA_HOME%;C:Windowssystem32;C:Windows;...





          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "3"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1390103%2fchanging-java-home-in-cmd-temporarily-doesnt-change-path-temporarily-in-windows%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            It doesn't change value in path or java -version doesn't change in current instance of cmd



            You have to restart cmd for the changes to take effect as a cmd shell inherits it environment from the parent process.





            So what is the correct way to switch between Java versions from the command line?



            Use a set of batch files, as follows:




            Being a Java developer, I always compile and test my code on different Java versions. But switching between them is a huge problem. So finally I found an easy method to do this. You have to create following batch files and place them in directory you open your command line in or in SYSTEM PATH. You can use you favorite text editor to create these files.



            jdk14.bat



            @echo off
            echo Setting JAVA_HOME
            set JAVA_HOME=C:j2sdk1.4.2_12
            echo setting PATH
            set PATH=C:j2sdk1.4.2_12bin;%PATH%
            echo Display java version
            java -version


            jdk15.bat



            @echo off
            echo Setting JAVA_HOME
            set JAVA_HOME=C:Program FilesJavajdk1.5.0_12
            echo setting PATH
            set PATH=C:Program FilesJavajdk1.5.0_12bin;%PATH%
            echo Display java version
            java -version


            jdk16.bat



            @echo off
            echo Setting JAVA_HOME
            set JAVA_HOME=C:Program FilesJavajdk1.6.0_11
            echo setting PATH
            set PATH=C:Program FilesJavajdk1.6.0_11bin;%PATH%
            echo Display java version
            java -version


            Make sure you assign the appropriate JAVA_HOME value in batch files, according to your Java installation. Whenever you want to switch between Java versions, just run the respective batch file and you are done.



            Note: JAVA_HOME and the path to java must always refer to the exact same version of the JDK. If you mix them up, unpredictable things will happen!




            Source Switch between different JDK versions in Windows | Oracle Pranav's Blog






            share|improve this answer


























            • but on restarting the temporary value of JAVA_HOME will be lost

              – the1derer
              Jan 3 at 10:00













            • You have to set it permanently and then change it back when you are done.

              – DavidPostill
              Jan 3 at 10:01











            • Answer updated.

              – DavidPostill
              Jan 3 at 10:06
















            2














            It doesn't change value in path or java -version doesn't change in current instance of cmd



            You have to restart cmd for the changes to take effect as a cmd shell inherits it environment from the parent process.





            So what is the correct way to switch between Java versions from the command line?



            Use a set of batch files, as follows:




            Being a Java developer, I always compile and test my code on different Java versions. But switching between them is a huge problem. So finally I found an easy method to do this. You have to create following batch files and place them in directory you open your command line in or in SYSTEM PATH. You can use you favorite text editor to create these files.



            jdk14.bat



            @echo off
            echo Setting JAVA_HOME
            set JAVA_HOME=C:j2sdk1.4.2_12
            echo setting PATH
            set PATH=C:j2sdk1.4.2_12bin;%PATH%
            echo Display java version
            java -version


            jdk15.bat



            @echo off
            echo Setting JAVA_HOME
            set JAVA_HOME=C:Program FilesJavajdk1.5.0_12
            echo setting PATH
            set PATH=C:Program FilesJavajdk1.5.0_12bin;%PATH%
            echo Display java version
            java -version


            jdk16.bat



            @echo off
            echo Setting JAVA_HOME
            set JAVA_HOME=C:Program FilesJavajdk1.6.0_11
            echo setting PATH
            set PATH=C:Program FilesJavajdk1.6.0_11bin;%PATH%
            echo Display java version
            java -version


            Make sure you assign the appropriate JAVA_HOME value in batch files, according to your Java installation. Whenever you want to switch between Java versions, just run the respective batch file and you are done.



            Note: JAVA_HOME and the path to java must always refer to the exact same version of the JDK. If you mix them up, unpredictable things will happen!




            Source Switch between different JDK versions in Windows | Oracle Pranav's Blog






            share|improve this answer


























            • but on restarting the temporary value of JAVA_HOME will be lost

              – the1derer
              Jan 3 at 10:00













            • You have to set it permanently and then change it back when you are done.

              – DavidPostill
              Jan 3 at 10:01











            • Answer updated.

              – DavidPostill
              Jan 3 at 10:06














            2












            2








            2







            It doesn't change value in path or java -version doesn't change in current instance of cmd



            You have to restart cmd for the changes to take effect as a cmd shell inherits it environment from the parent process.





            So what is the correct way to switch between Java versions from the command line?



            Use a set of batch files, as follows:




            Being a Java developer, I always compile and test my code on different Java versions. But switching between them is a huge problem. So finally I found an easy method to do this. You have to create following batch files and place them in directory you open your command line in or in SYSTEM PATH. You can use you favorite text editor to create these files.



            jdk14.bat



            @echo off
            echo Setting JAVA_HOME
            set JAVA_HOME=C:j2sdk1.4.2_12
            echo setting PATH
            set PATH=C:j2sdk1.4.2_12bin;%PATH%
            echo Display java version
            java -version


            jdk15.bat



            @echo off
            echo Setting JAVA_HOME
            set JAVA_HOME=C:Program FilesJavajdk1.5.0_12
            echo setting PATH
            set PATH=C:Program FilesJavajdk1.5.0_12bin;%PATH%
            echo Display java version
            java -version


            jdk16.bat



            @echo off
            echo Setting JAVA_HOME
            set JAVA_HOME=C:Program FilesJavajdk1.6.0_11
            echo setting PATH
            set PATH=C:Program FilesJavajdk1.6.0_11bin;%PATH%
            echo Display java version
            java -version


            Make sure you assign the appropriate JAVA_HOME value in batch files, according to your Java installation. Whenever you want to switch between Java versions, just run the respective batch file and you are done.



            Note: JAVA_HOME and the path to java must always refer to the exact same version of the JDK. If you mix them up, unpredictable things will happen!




            Source Switch between different JDK versions in Windows | Oracle Pranav's Blog






            share|improve this answer















            It doesn't change value in path or java -version doesn't change in current instance of cmd



            You have to restart cmd for the changes to take effect as a cmd shell inherits it environment from the parent process.





            So what is the correct way to switch between Java versions from the command line?



            Use a set of batch files, as follows:




            Being a Java developer, I always compile and test my code on different Java versions. But switching between them is a huge problem. So finally I found an easy method to do this. You have to create following batch files and place them in directory you open your command line in or in SYSTEM PATH. You can use you favorite text editor to create these files.



            jdk14.bat



            @echo off
            echo Setting JAVA_HOME
            set JAVA_HOME=C:j2sdk1.4.2_12
            echo setting PATH
            set PATH=C:j2sdk1.4.2_12bin;%PATH%
            echo Display java version
            java -version


            jdk15.bat



            @echo off
            echo Setting JAVA_HOME
            set JAVA_HOME=C:Program FilesJavajdk1.5.0_12
            echo setting PATH
            set PATH=C:Program FilesJavajdk1.5.0_12bin;%PATH%
            echo Display java version
            java -version


            jdk16.bat



            @echo off
            echo Setting JAVA_HOME
            set JAVA_HOME=C:Program FilesJavajdk1.6.0_11
            echo setting PATH
            set PATH=C:Program FilesJavajdk1.6.0_11bin;%PATH%
            echo Display java version
            java -version


            Make sure you assign the appropriate JAVA_HOME value in batch files, according to your Java installation. Whenever you want to switch between Java versions, just run the respective batch file and you are done.



            Note: JAVA_HOME and the path to java must always refer to the exact same version of the JDK. If you mix them up, unpredictable things will happen!




            Source Switch between different JDK versions in Windows | Oracle Pranav's Blog







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 3 at 10:06

























            answered Jan 3 at 9:54









            DavidPostillDavidPostill

            105k25227262




            105k25227262













            • but on restarting the temporary value of JAVA_HOME will be lost

              – the1derer
              Jan 3 at 10:00













            • You have to set it permanently and then change it back when you are done.

              – DavidPostill
              Jan 3 at 10:01











            • Answer updated.

              – DavidPostill
              Jan 3 at 10:06



















            • but on restarting the temporary value of JAVA_HOME will be lost

              – the1derer
              Jan 3 at 10:00













            • You have to set it permanently and then change it back when you are done.

              – DavidPostill
              Jan 3 at 10:01











            • Answer updated.

              – DavidPostill
              Jan 3 at 10:06

















            but on restarting the temporary value of JAVA_HOME will be lost

            – the1derer
            Jan 3 at 10:00







            but on restarting the temporary value of JAVA_HOME will be lost

            – the1derer
            Jan 3 at 10:00















            You have to set it permanently and then change it back when you are done.

            – DavidPostill
            Jan 3 at 10:01





            You have to set it permanently and then change it back when you are done.

            – DavidPostill
            Jan 3 at 10:01













            Answer updated.

            – DavidPostill
            Jan 3 at 10:06





            Answer updated.

            – DavidPostill
            Jan 3 at 10:06













            0














            The reason for this is that the variable reference in PATH is expanded at the time of the assignment to PATH, any later changes are ignored, the reference to the original value is lost. It's like making a copy of the value of the variable, not creating a reference to the variable.



            set JAVA_HOME=C:dir1
            PATH=%JAVA_HOME%
            set JAVA_HOME=C:dir2
            PATH


            This will output C:dir1 (value of JAVA_HOME at the time of the assignment) and not %JAVA_HOME%.



            You need a script like this to call after a change to JAVA_HOME:



            PATH=%JAVA_HOME%;C:Windowssystem32;C:Windows;...





            share|improve this answer




























              0














              The reason for this is that the variable reference in PATH is expanded at the time of the assignment to PATH, any later changes are ignored, the reference to the original value is lost. It's like making a copy of the value of the variable, not creating a reference to the variable.



              set JAVA_HOME=C:dir1
              PATH=%JAVA_HOME%
              set JAVA_HOME=C:dir2
              PATH


              This will output C:dir1 (value of JAVA_HOME at the time of the assignment) and not %JAVA_HOME%.



              You need a script like this to call after a change to JAVA_HOME:



              PATH=%JAVA_HOME%;C:Windowssystem32;C:Windows;...





              share|improve this answer


























                0












                0








                0







                The reason for this is that the variable reference in PATH is expanded at the time of the assignment to PATH, any later changes are ignored, the reference to the original value is lost. It's like making a copy of the value of the variable, not creating a reference to the variable.



                set JAVA_HOME=C:dir1
                PATH=%JAVA_HOME%
                set JAVA_HOME=C:dir2
                PATH


                This will output C:dir1 (value of JAVA_HOME at the time of the assignment) and not %JAVA_HOME%.



                You need a script like this to call after a change to JAVA_HOME:



                PATH=%JAVA_HOME%;C:Windowssystem32;C:Windows;...





                share|improve this answer













                The reason for this is that the variable reference in PATH is expanded at the time of the assignment to PATH, any later changes are ignored, the reference to the original value is lost. It's like making a copy of the value of the variable, not creating a reference to the variable.



                set JAVA_HOME=C:dir1
                PATH=%JAVA_HOME%
                set JAVA_HOME=C:dir2
                PATH


                This will output C:dir1 (value of JAVA_HOME at the time of the assignment) and not %JAVA_HOME%.



                You need a script like this to call after a change to JAVA_HOME:



                PATH=%JAVA_HOME%;C:Windowssystem32;C:Windows;...






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 3 at 11:12









                RalfFriedlRalfFriedl

                1,108147




                1,108147






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Super User!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1390103%2fchanging-java-home-in-cmd-temporarily-doesnt-change-path-temporarily-in-windows%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Сан-Квентин

                    8-я гвардейская общевойсковая армия

                    Алькесар