在使用IntelliJ IDEA 14.1.1开发项目的过程中,因为公司,生产,个人的问题,所以机器上安装JDK1.5~1.8的版本。但是只要指定使用JDK1.5时,Ctrl+Shift+F9编译某一个Java文件,编译结果就有提示:
JDK 1.5
Information:Using javac 1.8.0_40 to compile java sources
Information:4/3/2015 10:08 AM - Compilation completed successfully with 3 warnings in 1s 996ms
Warning:java: source value 1.5 is obsolete and will be removed in a future release
Warning:java: target value 1.5 is obsolete and will be removed in a future release
Warning:java: To suppress warnings about obsolete options, use -Xlint:-options.
不知为何调用了JDK1.8去编译文件,但是改为JDK1.6~1.8,编译结果提示:
JDK 1.6
Information:Using javac 1.6.0_45 to compile java sources
Information:4/3/2015 10:01 AM - Compilation completed successfully in 2s 546ms
JDK 1.7
Information:Using javac 1.7.0_75 to compile java sources
Information:4/3/2015 10:02 AM - Compilation completed successfully in 1s 837ms
JDK 1.8
Information:Using javac 1.8.0_40 to compile java sources
Information:4/3/2015 10:06 AM - Compilation completed successfully in 3s 283ms
IDEA编译环境如下:
1.在IDEA中将1.5~1.8都加入到Platform Settings下的SDKs中
2.在Project Settings的Project里面将项目SDK设置为1.5,语言级别也设置为5.0
3.Modules中将模块SDK也设置为1.5,语言级别也设置为5.0
4.在Complier中的Java Complier中将项目字节码也设置为1.5版本
只要将设置中的1.5和5.0替换为1.6~1.8/6~8,编译结果一切正常
另外将IDEA中的JDK1.8不配置进1中,仍然会提示使用1.8编译同时给出1.5警告
但是机器本身的环境变量中Java是使用的JDK1.7,不知道是我哪里环境配错了还是什么原因,还望指点。
追加:
经过验证,使用JDK1.5编译时,IDEA会调用配置给IDEA运行的JDK来编译文件,而忽略掉JDK1.5;非1.5则不会有这个问题,具体原因不清楚,不知是我的机器问题,还是IDEA 14已经Bug或是不支持JDK1.5了。
参考:JEP 182: Policy for Retiring javac -source and -target Options
我摘取了 Summary 中的一句话:
大概意思是,JDK9 开始不再支持 1.5 及之前版本的 source 和 target;JDK8 开始进行警告(以便于开发者及有足够的时间做好准备)
如果你确实需要设置成 1.5,加个编译参数 -Xlint:-options 来隐藏警告。