ClassNotFoundException: org.apache.hive.jdbc.HiveD

2019-02-21 19:14发布

问题:

I'm pretty new to Java. I'm trying to connect to hive server through java and used sample code from https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients#HiveServer2Clients-JDBC

import java.sql.SQLException;

public class HiveJdbcClient {
    //private static String driverName = "org.apache.hive.jdbc.HiveDriver";
    public static void main(String[] args) throws SQLException {
        try {
            Class.forName("org.apache.hive.jdbc.HiveDriver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.exit(1);
        }
    }
}

I placed all the jars in the required location and updated the pom file, but getting

java.lang.ClassNotFoundException: org.apache.hive.jdbc.HiveDriver
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at HiveJdbcClient.main(HiveJdbcClient.java:7)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

I searched for a solution for quite some time, but couldn't solve it. Please let me know how to fix this.

回答1:

This is due to mismatch in hivesever2 version.If Hive version is more than 0.13 then You may have to use this.

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

Also make sure you add this jar in you class path..



回答2:

You need to include a library in your project (JAR file) which includes that missing class org.apache.hive.jdbc.HiveDriver. Here it is a link to version 0.8.0 of it.



回答3:

From the stack trace you posted I am assuming that you are running this through IntelliJ and getting this error.

The POM describes how to build the project not how to execute the compiled project. In your class you do not import org.apache.hive.jdbc.HiveDriver so I imagine that IntelliJ is not going to ensure that its containing JAR is passed to the JVM on the classpath.

What I believe you have to do in this case is manually pass in the location of the hive jar on the classpath. Somewhere in the project settings (NOT the POM) in your IDE where will be runtime settings, you will need to include the cp or -classpath commandline switch which will point to the hive JAR. Or alternatively you can do as David Fernadez says and import the class which should force IntelliJ to pass the JAR in on the classpath.



回答4:

I guess the reason is that the Java compiler or JVM doesn't read classpath as supposed.

The environment setting is really difficult for beginners. I will recommend using Cloudera quickstart VM and Java IDE to ease the pain.

I suffered many hours and organized it in this article: http://www.yuchao.us/2017/05/hive-jdbc-in-cloudera-hadoop.html