遇到一个Java相关的报错:
...
java.lang.UnsupportedClassVersionError: ... : Unsupported major.minor version 52.0
...
报错说的不明不白,度娘后发现这样一个消息:
Whenever something like this happens in our software, we tell our users. This is a bug, we need to fix it.
I would expect a message like: You are running the wrong Java version on this machine: 1.7, please install and set the right java version: 1.8
A normal user will first have to find out what a major.minor version is, then have to find the mapping between majaor.minor and Java version,
depending on vendor, this could even vary. This is not a good user experience.
Version-Release number of selected component (if applicable):
How reproducible:
always
Steps to Reproduce:
1. Install and set java 7: java -version should show 1.7
2. Run Java 8 compiled code against this java version
检查看发现自己服务器上Java果然是1.7的(Java -version)
下载合适的Java1.8:
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
安装:
[root@hadoop-node-2 java]# rpm -ivh jdk-8u92-linux-x64.rpm
Preparing... ########################################### [100%]
1:jdk1.8.0_92 ########################################### [100%]
Unpacking JAR files...
tools.jar...
plugin.jar...
javaws.jar...
deploy.jar...
rt.jar...
jsse.jar...
charsets.jar...
localedata.jar...
添加环境变量:
[root@hadoop-node-2 jdk1.8.0_92]# tail -3 /etc/profile
export JAVA_HOME=/usr/java/jdk1.8.0_92
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
查看是否成功:
[root@hadoop-node-2 jdk1.8.0_92]# which java
/usr/java/jdk1.8.0_92/bin/java
[root@hadoop-node-2 jdk1.8.0_92]# which javac
/usr/java/jdk1.8.0_92/bin/javac
[root@hadoop-node-2 jdk1.8.0_92]# java -version
java version "1.8.0_92"
Java(TM) SE Runtime Environment (build 1.8.0_92-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.92-b14, mixed mode)
[root@hadoop-node-2 jdk1.8.0_92]# javac -version
javac 1.8.0_92
再次运行我的程序,成功通过,不再报错,完美!
另附相关的几个近义词辨析:
Java:是一种计算机编程语言
javac:是Java语言编程编译器,是一种可以读取由java语言编写的类和接口的定义,并将它们编译成字节代码的class文件的工具
JDK:是Java语言的软件开发工具包,没有JDK的话,无法编译Java程序
jre: 为Java Runtime Environment的简称,即Java运行环境,是运行JAVA程序所必须的环境的集合,它包括Java虚拟机(jvm)、Java核心类库和支持文件,但不包含开发工具(JDK)、调试器和其它工具。
注意:如果只需要运行Java程序或Applet,下载并安装jre即可。如果要自行开发Java软件,请下载JDK。在JDK中附带有JRE