setting alias for Java9
Clash 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.
bash shell-script bashrc
add a comment |Â
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.
bash shell-script bashrc
add a comment |Â
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.
bash shell-script bashrc
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
bash shell-script bashrc
asked Oct 8 '17 at 17:26
Lovey
83
83
add a comment |Â
add a comment |Â
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.
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, thensource
that file. That would make the function available. If you make changes to the file, you will have tosource
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
add a comment |Â
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.
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, thensource
that file. That would make the function available. If you make changes to the file, you will have tosource
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
add a comment |Â
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.
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, thensource
that file. That would make the function available. If you make changes to the file, you will have tosource
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
add a comment |Â
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.
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.
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, thensource
that file. That would make the function available. If you make changes to the file, you will have tosource
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
add a comment |Â
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, thensource
that file. That would make the function available. If you make changes to the file, you will have tosource
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password