setting alias for Java9

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I am new to bash. I have multiple java versions installed on my machine and I want to create an alias for all of them. Typically the executable for java is in folder /Library//bin/java. I want to create and alias so that I type java9 in bash and it executes executable for java9 (ad similarly for other java versions). How to do this.



Note typically for one javaversion I add JAVA_HOME and append the path in PATH. But not sure how to do this.



I was reading about this and looks like eval could be an option, but not sure how to do this.










share|improve this question

























    up vote
    1
    down vote

    favorite












    I am new to bash. I have multiple java versions installed on my machine and I want to create an alias for all of them. Typically the executable for java is in folder /Library//bin/java. I want to create and alias so that I type java9 in bash and it executes executable for java9 (ad similarly for other java versions). How to do this.



    Note typically for one javaversion I add JAVA_HOME and append the path in PATH. But not sure how to do this.



    I was reading about this and looks like eval could be an option, but not sure how to do this.










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am new to bash. I have multiple java versions installed on my machine and I want to create an alias for all of them. Typically the executable for java is in folder /Library//bin/java. I want to create and alias so that I type java9 in bash and it executes executable for java9 (ad similarly for other java versions). How to do this.



      Note typically for one javaversion I add JAVA_HOME and append the path in PATH. But not sure how to do this.



      I was reading about this and looks like eval could be an option, but not sure how to do this.










      share|improve this question













      I am new to bash. I have multiple java versions installed on my machine and I want to create an alias for all of them. Typically the executable for java is in folder /Library//bin/java. I want to create and alias so that I type java9 in bash and it executes executable for java9 (ad similarly for other java versions). How to do this.



      Note typically for one javaversion I add JAVA_HOME and append the path in PATH. But not sure how to do this.



      I was reading about this and looks like eval could be an option, but not sure how to do this.







      bash shell-script bashrc






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Oct 8 '17 at 17:26









      Lovey

      83




      83




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          javav () 
          local version="$1"
          shift

          case "$version" in
          8) JAVA_HOME='/Library/some/path/java'
          PATH="$JAVA_HOME/bin:$PATH" java "$@"
          ;;
          8.7) JAVA_HOME='/Library/some/other/path/java'
          PATH="$JAVA_HOME/bin:$PATH" java "$@"
          ;;
          9) JAVA_HOME='/Library/some/other/path2/java'
          PATH="$JAVA_HOME/bin:$PATH" java "$@"
          ;;
          *) printf 'Unknown java version: %sn' "$version" >&2
          return 1
          esac



          This is a shell function, javav (rename it if it clashes with any existing tool). You would put this in your .bash_profile or .bashrc file, or in its own file (in which case you would need to source it with source filename).



          It takes a version number as its first argument and sets JAVA_HOME to the path of the appropriate directory, and PATH to include $JAVA_HOME/bin in the beginning before executing java with the remaining command line arguments. Since the java executable presumably lives in $JAVA_HOME/bin and since this is first in the PATH, that's the Java version that will be started.



          This means that you use this like so:



          $ javav 9 other arguments to java here


          Assuming the function has been set up with the correct JAVA_HOME directory and PATH, this ought to launch Java 9 and pass other arguments to java here to the java executable.



          Note: I'm not a Java developer, and I don't know if makes sense to set PATH the way I'm doing above, but you can modify it to fit your needs.






          share|improve this answer






















          • Thanksfor the answer. I added it says javav not found. Tried to load it with source command still same error.
            – Lovey
            Oct 9 '17 at 20:07










          • @Lovey Put it in its own file, then source that file. That would make the function available. If you make changes to the file, you will have to source it again. You would also need to update it with the proper paths of course.
            – Kusalananda
            Oct 9 '17 at 20:34










          • Thanks It worked. Is there a way to load this function to bash profile, so that I will not have to source this everytime, whenever I want to switch env.
            – Lovey
            Oct 9 '17 at 21:00











          • @Lovey You could add it in .bash_profile directly, or source it from there (or from .bashrc).
            – Kusalananda
            Oct 9 '17 at 21:22










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f396876%2fsetting-alias-for-java9%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          javav () 
          local version="$1"
          shift

          case "$version" in
          8) JAVA_HOME='/Library/some/path/java'
          PATH="$JAVA_HOME/bin:$PATH" java "$@"
          ;;
          8.7) JAVA_HOME='/Library/some/other/path/java'
          PATH="$JAVA_HOME/bin:$PATH" java "$@"
          ;;
          9) JAVA_HOME='/Library/some/other/path2/java'
          PATH="$JAVA_HOME/bin:$PATH" java "$@"
          ;;
          *) printf 'Unknown java version: %sn' "$version" >&2
          return 1
          esac



          This is a shell function, javav (rename it if it clashes with any existing tool). You would put this in your .bash_profile or .bashrc file, or in its own file (in which case you would need to source it with source filename).



          It takes a version number as its first argument and sets JAVA_HOME to the path of the appropriate directory, and PATH to include $JAVA_HOME/bin in the beginning before executing java with the remaining command line arguments. Since the java executable presumably lives in $JAVA_HOME/bin and since this is first in the PATH, that's the Java version that will be started.



          This means that you use this like so:



          $ javav 9 other arguments to java here


          Assuming the function has been set up with the correct JAVA_HOME directory and PATH, this ought to launch Java 9 and pass other arguments to java here to the java executable.



          Note: I'm not a Java developer, and I don't know if makes sense to set PATH the way I'm doing above, but you can modify it to fit your needs.






          share|improve this answer






















          • Thanksfor the answer. I added it says javav not found. Tried to load it with source command still same error.
            – Lovey
            Oct 9 '17 at 20:07










          • @Lovey Put it in its own file, then source that file. That would make the function available. If you make changes to the file, you will have to source it again. You would also need to update it with the proper paths of course.
            – Kusalananda
            Oct 9 '17 at 20:34










          • Thanks It worked. Is there a way to load this function to bash profile, so that I will not have to source this everytime, whenever I want to switch env.
            – Lovey
            Oct 9 '17 at 21:00











          • @Lovey You could add it in .bash_profile directly, or source it from there (or from .bashrc).
            – Kusalananda
            Oct 9 '17 at 21:22














          up vote
          1
          down vote



          accepted










          javav () 
          local version="$1"
          shift

          case "$version" in
          8) JAVA_HOME='/Library/some/path/java'
          PATH="$JAVA_HOME/bin:$PATH" java "$@"
          ;;
          8.7) JAVA_HOME='/Library/some/other/path/java'
          PATH="$JAVA_HOME/bin:$PATH" java "$@"
          ;;
          9) JAVA_HOME='/Library/some/other/path2/java'
          PATH="$JAVA_HOME/bin:$PATH" java "$@"
          ;;
          *) printf 'Unknown java version: %sn' "$version" >&2
          return 1
          esac



          This is a shell function, javav (rename it if it clashes with any existing tool). You would put this in your .bash_profile or .bashrc file, or in its own file (in which case you would need to source it with source filename).



          It takes a version number as its first argument and sets JAVA_HOME to the path of the appropriate directory, and PATH to include $JAVA_HOME/bin in the beginning before executing java with the remaining command line arguments. Since the java executable presumably lives in $JAVA_HOME/bin and since this is first in the PATH, that's the Java version that will be started.



          This means that you use this like so:



          $ javav 9 other arguments to java here


          Assuming the function has been set up with the correct JAVA_HOME directory and PATH, this ought to launch Java 9 and pass other arguments to java here to the java executable.



          Note: I'm not a Java developer, and I don't know if makes sense to set PATH the way I'm doing above, but you can modify it to fit your needs.






          share|improve this answer






















          • Thanksfor the answer. I added it says javav not found. Tried to load it with source command still same error.
            – Lovey
            Oct 9 '17 at 20:07










          • @Lovey Put it in its own file, then source that file. That would make the function available. If you make changes to the file, you will have to source it again. You would also need to update it with the proper paths of course.
            – Kusalananda
            Oct 9 '17 at 20:34










          • Thanks It worked. Is there a way to load this function to bash profile, so that I will not have to source this everytime, whenever I want to switch env.
            – Lovey
            Oct 9 '17 at 21:00











          • @Lovey You could add it in .bash_profile directly, or source it from there (or from .bashrc).
            – Kusalananda
            Oct 9 '17 at 21:22












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          javav () 
          local version="$1"
          shift

          case "$version" in
          8) JAVA_HOME='/Library/some/path/java'
          PATH="$JAVA_HOME/bin:$PATH" java "$@"
          ;;
          8.7) JAVA_HOME='/Library/some/other/path/java'
          PATH="$JAVA_HOME/bin:$PATH" java "$@"
          ;;
          9) JAVA_HOME='/Library/some/other/path2/java'
          PATH="$JAVA_HOME/bin:$PATH" java "$@"
          ;;
          *) printf 'Unknown java version: %sn' "$version" >&2
          return 1
          esac



          This is a shell function, javav (rename it if it clashes with any existing tool). You would put this in your .bash_profile or .bashrc file, or in its own file (in which case you would need to source it with source filename).



          It takes a version number as its first argument and sets JAVA_HOME to the path of the appropriate directory, and PATH to include $JAVA_HOME/bin in the beginning before executing java with the remaining command line arguments. Since the java executable presumably lives in $JAVA_HOME/bin and since this is first in the PATH, that's the Java version that will be started.



          This means that you use this like so:



          $ javav 9 other arguments to java here


          Assuming the function has been set up with the correct JAVA_HOME directory and PATH, this ought to launch Java 9 and pass other arguments to java here to the java executable.



          Note: I'm not a Java developer, and I don't know if makes sense to set PATH the way I'm doing above, but you can modify it to fit your needs.






          share|improve this answer














          javav () 
          local version="$1"
          shift

          case "$version" in
          8) JAVA_HOME='/Library/some/path/java'
          PATH="$JAVA_HOME/bin:$PATH" java "$@"
          ;;
          8.7) JAVA_HOME='/Library/some/other/path/java'
          PATH="$JAVA_HOME/bin:$PATH" java "$@"
          ;;
          9) JAVA_HOME='/Library/some/other/path2/java'
          PATH="$JAVA_HOME/bin:$PATH" java "$@"
          ;;
          *) printf 'Unknown java version: %sn' "$version" >&2
          return 1
          esac



          This is a shell function, javav (rename it if it clashes with any existing tool). You would put this in your .bash_profile or .bashrc file, or in its own file (in which case you would need to source it with source filename).



          It takes a version number as its first argument and sets JAVA_HOME to the path of the appropriate directory, and PATH to include $JAVA_HOME/bin in the beginning before executing java with the remaining command line arguments. Since the java executable presumably lives in $JAVA_HOME/bin and since this is first in the PATH, that's the Java version that will be started.



          This means that you use this like so:



          $ javav 9 other arguments to java here


          Assuming the function has been set up with the correct JAVA_HOME directory and PATH, this ought to launch Java 9 and pass other arguments to java here to the java executable.



          Note: I'm not a Java developer, and I don't know if makes sense to set PATH the way I'm doing above, but you can modify it to fit your needs.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 8 '17 at 18:01

























          answered Oct 8 '17 at 17:41









          Kusalananda

          105k14209326




          105k14209326











          • Thanksfor the answer. I added it says javav not found. Tried to load it with source command still same error.
            – Lovey
            Oct 9 '17 at 20:07










          • @Lovey Put it in its own file, then source that file. That would make the function available. If you make changes to the file, you will have to source it again. You would also need to update it with the proper paths of course.
            – Kusalananda
            Oct 9 '17 at 20:34










          • Thanks It worked. Is there a way to load this function to bash profile, so that I will not have to source this everytime, whenever I want to switch env.
            – Lovey
            Oct 9 '17 at 21:00











          • @Lovey You could add it in .bash_profile directly, or source it from there (or from .bashrc).
            – Kusalananda
            Oct 9 '17 at 21:22
















          • Thanksfor the answer. I added it says javav not found. Tried to load it with source command still same error.
            – Lovey
            Oct 9 '17 at 20:07










          • @Lovey Put it in its own file, then source that file. That would make the function available. If you make changes to the file, you will have to source it again. You would also need to update it with the proper paths of course.
            – Kusalananda
            Oct 9 '17 at 20:34










          • Thanks It worked. Is there a way to load this function to bash profile, so that I will not have to source this everytime, whenever I want to switch env.
            – Lovey
            Oct 9 '17 at 21:00











          • @Lovey You could add it in .bash_profile directly, or source it from there (or from .bashrc).
            – Kusalananda
            Oct 9 '17 at 21:22















          Thanksfor the answer. I added it says javav not found. Tried to load it with source command still same error.
          – Lovey
          Oct 9 '17 at 20:07




          Thanksfor the answer. I added it says javav not found. Tried to load it with source command still same error.
          – Lovey
          Oct 9 '17 at 20:07












          @Lovey Put it in its own file, then source that file. That would make the function available. If you make changes to the file, you will have to source it again. You would also need to update it with the proper paths of course.
          – Kusalananda
          Oct 9 '17 at 20:34




          @Lovey Put it in its own file, then source that file. That would make the function available. If you make changes to the file, you will have to source it again. You would also need to update it with the proper paths of course.
          – Kusalananda
          Oct 9 '17 at 20:34












          Thanks It worked. Is there a way to load this function to bash profile, so that I will not have to source this everytime, whenever I want to switch env.
          – Lovey
          Oct 9 '17 at 21:00





          Thanks It worked. Is there a way to load this function to bash profile, so that I will not have to source this everytime, whenever I want to switch env.
          – Lovey
          Oct 9 '17 at 21:00













          @Lovey You could add it in .bash_profile directly, or source it from there (or from .bashrc).
          – Kusalananda
          Oct 9 '17 at 21:22




          @Lovey You could add it in .bash_profile directly, or source it from there (or from .bashrc).
          – Kusalananda
          Oct 9 '17 at 21:22

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f396876%2fsetting-alias-for-java9%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay