软件工程日报
遇到了 Java 版本不兼容的问题。错误信息显示:
UnsupportedClassVersionError: com/example/hbase_test_new/HbaseTestNewApplication has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 52.0
这意味着项目是用 Java 17(class file version 61.0)编译的,但运行时使用的是 Java 8(class file version 52.0)。
解决方案
方案1:统一使用 Java 8(推荐)
1. 在 IntelliJ IDEA 中设置 Java 8
-
打开项目结构设置:
- File → Project Structure
- 在 Project 设置中:
- Project SDK: 选择 Java 8
- Project language level: 选择 8
-
设置模块的 SDK:
- 在 Modules 中,确保所有模块都使用 Java 8
-
设置运行配置:
- Run → Edit Configurations
- 确保您的运行配置使用 Java 8
2. 在 Maven 中配置 Java 8
在 pom.xml 中添加:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
</properties>
3. 重新编译项目
在终端中运行:
mvn clean compile
方案2:如果您想使用 Java 17
如果您想使用 Java 17,需要确保:
- HBase 服务器版本支持 Java 17(HBase 2.1.3 可能不完全支持 Java 17)
- 更新所有依赖版本
完整的 HBase Java 8 兼容代码
这里是针对 Java 8 和 HBase 2.1.3 的完整兼容代码:
package com.example.hbase_test_new;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class Practice4 {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void main(String[] args) throws IOException {
init();
createTable("student", new String[]{"score"});
insertData("student", "zhangsan", "score", "English", "69");
insertData("student", "zhangsan", "score", "Math", "86");
insertData("student", "zhangsan", "score", "Computer", "77");
getData("student", "zhangsan", "score", "English");
close();
}
public static void init() {
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "localhost");
configuration.set("hbase.zookeeper.property.clientPort", "2181");
// 添加超时配置
configuration.set("hbase.rpc.timeout", "10000");
configuration.set("hbase.client.operation.timeout", "30000");
configuration.set("hbase.client.scanner.timeout.period", "10000");
// Windows 兼容性设置
System.setProperty("hadoop.home.dir", "C:\\tmp\\hadoop");
try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
System.out.println("HBase 连接成功!");
} catch (IOException e) {
System.err.println("HBase 连接失败: " + e.getMessage());
e.printStackTrace();
}
}
public static void close() {
try {
if (admin != null) {
admin.close();
}
if (connection != null) {
connection.close();
}
System.out.println("HBase 连接已关闭");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void createTable(String myTableName, String[] colFamily) throws IOException {
TableName tableName = TableName.valueOf(myTableName);
if (admin.tableExists(tableName)) {
System.out.println("表 " + myTableName + " 已存在!");
} else {
// HBase 2.1.3 使用 HTableDescriptor(兼容旧API)
HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
for (String family : colFamily) {
HColumnDescriptor columnDescriptor = new HColumnDescriptor(family);
tableDescriptor.addFamily(columnDescriptor);
}
admin.createTable(tableDescriptor);
System.out.println("表 " + myTableName + " 创建成功!");
}
}
public static void insertData(String tableName, String rowKey, String colFamily, String col, String val) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
try {
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(val));
table.put(put);
System.out.println("插入数据成功: " + rowKey + " - " + colFamily + ":" + col + " = " + val);
} finally {
table.close();
}
}
public static void getData(String tableName, String rowKey, String colFamily, String col) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
try {
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col));
Result result = table.get(get);
byte[] value = result.getValue(Bytes.toBytes(colFamily), Bytes.toBytes(col));
if (value != null) {
System.out.println("查询结果: " + new String(value));
} else {
System.out.println("未找到数据");
}
} finally {
table.close();
}
}
}
更新 Maven 依赖
确保您的 pom.xml 使用正确的 HBase 2.1.3 依赖:
<properties>
<hbase.version>2.1.3</hbase.version>
<hadoop.version>2.7.7</hadoop.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- HBase 2.1.3 依赖 -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>${hbase.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>${hbase.version}</version>
</dependency>
<!-- 其他依赖... -->
</dependencies>
解决步骤总结
- 在 IDEA 中设置 Java 8 为项目 SDK
- 在 pom.xml 中添加 Java 8 编译配置
- 使用上面的兼容代码
- 运行
mvn clean compile重新编译 - 运行程序
这样应该可以解决 Java 版本不兼容的问题。

浙公网安备 33010602011771号