Mac系统Java默认版本设置的相关问题

概览

参考链接:

https://www.oracle.com/search/results?cat=javacom&Ntk=SI-ALL5&lang=zhs&Ntt=jdk&submit.x=0&submit.y=0
https://cloud.tencent.com/developer/ask/146039
https://stackoverflow.com/questions/21964709/how-to-set-or-change-the-default-java-jdk-version-on-os-x
https://blog.adoptopenjdk.net/2018/10/macos-binary-changes
https://gist.github.com/hogmoru/8e02cf826c840914a8ed93fd418ed88e
https://www.java.com/zh_CN/download/help/mac_uninstall_java.xml
http://www.micmiu.com/lang/java/set-javahome-on-mac-os-x/
https://developer.apple.com/library/archive/qa/qa1170/_index.html
https://docs.oracle.com/javase/8/docs/technotes/guides/install/mac_jdk.html

关联问题:

  1. java_home版本与java -version不一致问题(java_home无法输出想要的版本路径,但是java -version显示为目的版本).
  2. Eclipse设置build path中jre环境无效.
  3. Eclipse中maven所有命令显示错误: 找不到或无法加载主类.
  4. Eclipse中运行main函数显示错误: 找不到或无法加载主类.

问题描述:

  1. 系统安装Java10之后,Eclipse项目修改build path设置Libraries中JRE System Library为Java8,结果出现错误: 找不到或无法加载主类
  2. 执行/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home显示为非Java8

问题排除过程:

1.系统安装了两个版本Java.
2.环境变量也改了(本人使用的的是oh-my-zsh,只需要在`~/.zshrc`中添加就行,保险起见`~/.bash_profile`中也添加).
  JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home
  PATH=$JAVA_HOME/bin:$PATH:.
  CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:.
  export JAVA_HOME
  export PATH
  export CLASSPATH
  1. 终端手动mvn install没有问题.
  2. /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java -version显示是java8,但是问题依然没有解决
  3. /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home依然是 java10
  4. /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home -V显示java10和java8都在,似乎可选,难道是java_home可以选择?,网上没有搜到除了-V以外的参数,咋整…
  5. 搜方案
    (1) export环境变量,能搜到的方案基本上就是export,全部对我无效.
    (2) 使用偏好设置中的的plugin设置默认版本,Majave版没发现有,安装java10的时候就没了,但是从官方页面
    https://java.com/zh_CN/download/help/mac_uninstall_java.xml提到的删除步骤中提到在/Library/PreferencePanes路径下有JavaControlPanel.prefPane,可我没有,并且Installer.plist显示是java8,没有java10,也应该没问题,所以此方案对我无效.
    (3) 删java10,太简单,有点low,体现不出作为程序员的自豪感,无效.
    (4) …搜了N个export方案…
    (5) 靠谱的解释慢慢出现,有人提到,Mac系统会自动检查java安装的最新版本,也就是部分解释了一些疑问,export输出的环境变量,与系统内部机制有出入,完犊子,我就改个java版本难道还得学个mac开发不成,就算去找关于/System/Library/Frameworks/JavaVM.framework/Versions/Current/的选择机制问题基本上是无效的,如果考虑使用ln命令重新链接?系统机制不了解,衍生问题会居多无比,解决问题的时间曲线就要炸了wtf.
    (6) …继续搜,全是export方案,就不能有点创意么(╯‵□′)╯︵┻━┻…
    (7) finally,搜到一个失败案例https://cloud.tencent.com/developer/ask/146039,案例提到(https://stackoverflow.com/questions/21964709/how-to-set-or-change-the-default-java-jdk-version-on-os-x),stackoverflow真是一个嗯哼的网站,要是英语不好,能折磨死你,很巧的是第二置顶方案说的很有意思,估计国内哥们儿肯定没往下看,第二置顶的方案说到很关键的内容:
  • leave all JDKs at their default location, under /Library/Java/JavaVirtualMachines. The system will pick the highest version by default.
  • To exclude a JDK from being picked by default, rename its Contents/Info.plist to Info.plist.disabled. That JDK can still be used when $JAVA_HOME points to it, or explicitly referenced in a script or configuration. It will simply be ignored by system’s java command.

问题搞定:

修改`/Library/Java/JavaVirtualMachines/jdk-10.jdk/Contents/Info.plist`为`/Library/Java/JavaVirtualMachines/jdk-10.jdk/Contents/Info.plist.disabled`
java_home显示为java8,一切问题解决

不完善部分:

1.`java_home -V`中的显示的可选择版本java10消失
2.java版本选择不灵活,需要单独去disabled掉plist文件
3.mac系统对java版本的选择机制,本质上运行原理还是不甚了解,只是笼统解决了问题,例如,系统是如何操作plist文件来实现默认选择版本的,无从知道
4.谁能解释下这个java_home是咋回事不,咋来的

备用解决方案

官方文档: link
Determining the Default Version of the JDK部分提到

If you have not yet installed Apple's Java OS X 2012-006 update, then you are still using a version of Apple Java 6 that includes the plug-in and the Java Preferences app. See "Note for Users of OS X that Include Apple Java 6 Plug-in".
There can be multiple JDKs installed on a system, as many as you wish.
When launching a Java application through the command line, the system uses the default JDK. It is possible for the version of the JRE to be different than the version of the JDK.
You can determine which version of the JDK is the default by typing java -version in a Terminal window. If the installed version is 8u6, you will see a string that includes the text 1.8.0_06. For example:

% java -version
java version "1.8.0_06-ea"
Java(TM) SE Runtime Environment (build 1.8.0_06-ea-b13)
Java HotSpot(TM) 64-Bit Server VM (build 23.2-b04, mixed mode)
To run a different version of Java, either specify the full path, or use the java_home tool:
% /usr/libexec/java_home -v 1.8.0_06 --exec javac -version
For more information, see the java_home(1) man page.

虽然案例是java6切换java8,仿照试验了一下/usr/libexec/java_home -v 1.8.0_191 --exec javac -version,貌似没有什么问题出现,但是java_home输出结果还是java10,Eclipse目前没有出现错误: 找不到或无法加载主类的情况.

➜  Commands /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home -v 1.8.0_191 --exec javac -version
javac 1.8.0_191
➜  Commands /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home      
/Library/Java/JavaVirtualMachines/jdk-10.jdk/Contents/Home
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值