0

When I try to run the code in IDEA I don't get this error, but after I package it and try to run the jar with

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

I get this exception.

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

My 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>

My code

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)");
  }
}

Why does this happen?

EDIT Full error

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)
Evgenii
  • 389
  • 3
  • 7
  • 21
  • You specify `-cp app.jar`, which means that none of its dependencies are on the current classpath. – Mark Rotteveel Aug 08 '17 at 09:20
  • @Mark thanks for pointing it out. checked how to make jar with dependencies here https://stackoverflow.com/questions/1729054/including-dependencies-in-a-jar-with-maven – Evgenii Aug 08 '17 at 09:44
  • And that can be a very brittle approach, especially with resources that might exist in multiple dependencies (eg service provider definitions); you might want to include the full stacktrace of the `ClassNotFoundException` – Mark Rotteveel Aug 08 '17 at 11:54
  • @Mark Brittle how? Added full error. – Evgenii Aug 09 '17 at 11:48

2 Answers2

0

Try like this:

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

instead of this:

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

The error is due to the non-availability of the Hive JDBC jar while trying to run your Java code (which inturn tries to load the Hive JDBC jar)

Hope this helps!

N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46
0

1-if you are using maven , you have to add this in pom.xml file

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

2.Alternatively you can download above mentioned jar.

  • Place the jar in to hive/install location/of/lib directory...

This properly working always.