包装和运行应用程序后,我得到java.lang.ClassNotFoundException:org.apache.hive.jdbc.HiveDriver

时间:2017-08-08 09:19:17

标签: java maven hadoop jdbc hive

当我尝试在IDEA中运行代码时,我没有收到此错误,但在我打包并尝试使用

运行jar之后
java -cp app.jar com.hive.connect.controller.RecordController

我得到了这个例外。

java.lang.ClassNotFoundException: org.apache.hive.jdbc.HiveDriver

我的maven

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.hive</groupId>
      <artifactId>hive-jdbc</artifactId>
      <version>1.1.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-core</artifactId>
      <version>1.2.1</version>
    </dependency>
  </dependencies>

我的代码

public class RecordController {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";

public static void main(String[] args) throws SQLException, ClassNotFoundException {
    Class.forName(driverName);
    Connection con = DriverManager
                       .getConnection("jdbc:hive2://host:port/db;" +
                                        "principal=principal");
    Statement stmt = con.createStatement();
    String tableName = "evkuzmin_testHiveDriverTable";
    stmt.execute("drop table if exists " + tableName);
    stmt.execute("create table " + tableName + " (key int, value string)");
  }
}

为什么会这样?

EDIT 完整错误

Exception in thread "main" java.lang.ClassNotFoundException: org.apache.hive.jdbc.HiveDriver
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:190)
        at com.hive.connect.controller.RecordController.main(RecordController.java:18)

2 个答案:

答案 0 :(得分:0)

试试这样:

"%JAVA_HOME%\bin\java.exe" -cp  "%~dp0\hive-jdbc.jar;%~dp0\app.jar" com.hive.connect.controller.RecordController

而不是:

java -cp app.jar com.hive.connect.controller.RecordController

该错误是由于在尝试运行Java代码时无法使用Hive JDBC jar(内部尝试加载Hive JDBC jar)

希望这有帮助!

答案 1 :(得分:0)

1 - 如果您使用的是maven,则必须在pom.xml文件中添加

<dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-jdbc</artifactId>
    <version>0.8.1</version>
</dependency>

2.您也可以下载上面提到的jar。

  • 将jar放入hive / install location / of / lib目录...

这始终正常工作。