5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

macのzshでjava7/8を簡単に切り替える

Posted at

以下のコードを~/.zshenvに追記しsourceしてjava7もしくはjava8すれば切り替えられます
Yosemiteとかで確認(雑

alias java7='setjdk 1.7'
alias java8='setjdk 1.8'
function setjdk() {
  if [ $# -ne 0 ]; then
    removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
    if [ -n "${JAVA_HOME+x}" ]; then
    removeFromPath $JAVA_HOME
    fi
    export JAVA_HOME=`/usr/libexec/java_home -v $@`
    export PATH=$JAVA_HOME/bin:$PATH
  fi
}
function removeFromPath() {
  export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}
setjdk 1.8

これで

java7のサポートもうすぐ切れるしイケてないから普段はjava8だけど、java7でしか動かないsbtプロジェクトとかあって時にはjava7が必要で、とは言え互換があるからといってデフォルトでjava7を使いたくないけど必要なときにjava8から簡単にjava7に切り替えたい

という人の要求を満たせますね。

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?