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.

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: %s\n' "$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.

  • 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

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.