Support Questions
Find answers, ask questions, and share your expertise

jdbc class not found error

Master Collaborator

I have the hive-jdbc.jar file but still the compiler cant find it .

-rw-r--r-- 1 root root 110242 Nov 29  2016 /usr/hdp/2.5.3.0-37/hive2/lib/hive-jdbc-2.1.0.2.5.3.0-37.jar
[hdfs@hadoop1 ~]$
[hdfs@hadoop1 ~]$ javac -cp /usr/hdp/2.5.3.0-37/hive2/lib/hive-jdbc.jar HiveAlterRenameTo.java
HiveAlterRenameTo.java:13: error: unreported exception ClassNotFoundException; must be caught or declared to be thrown
      Class.forName(driverName);
                   ^

code is as below:

[hdfs@hadoop1 ~]$ more HiveAlterRenameTo.java
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
public class HiveAlterRenameTo {
   private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
   public static void main(String[] args) throws SQLException {
      // Register driver and create driver instance
      Class.forName(driverName);
      // get connection
      Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/default", "", "");
      // create statement
      Statement stmt = con.createStatement();
      // execute statement
      stmt.executeQuery("ALTER TABLE tab1_ORC RENAME TO tab1;");
      System.out.println("Table Renamed Successfully");
      con.close();
   }
}
6 REPLIES 6

@Sami Ahmad Can you try

javac -cp `hadoop classpath`:/usr/hdp/2.5.3.0-37/hive2/lib/* HiveAlterRenameTo.java

Master Collaborator

still the same error

[hdfs@hadoop1 ~]$ env | grep HADOOP
HADOOP_CLASSPATH=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.111-0.b15.el6_8.x86_64/lib/tools.jar
[hdfs@hadoop1 ~]$
[hdfs@hadoop1 ~]$
[hdfs@hadoop1 ~]$ javac -cp $HADOOP_CLASSPATH:/usr/hdp/2.5.3.0-37/hive2/lib/*  HiveAlterRenameTo.java
HiveAlterRenameTo.java:13: error: unreported exception ClassNotFoundException; must be caught or declared to be thrown
      Class.forName(driverName);
                   ^
1 error
[hdfs@hadoop1 ~]$

Master Collaborator

Your driverName is set incorrectly, it should be "org.apache.hive.jdbc.HiveDriver".

Master Collaborator

still same error

[hdfs@hadoop1 ~]$ grep "driverName =" *.java
HiveAlterRenameTo.java:   private static String driverName = "org.apache.hive.jdbc.HiveDriver";
[hdfs@hadoop1 ~]$ echo $HADOOP_CLASSPATH
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.111-0.b15.el6_8.x86_64/lib/tools.jar
[hdfs@hadoop1 ~]$
[hdfs@hadoop1 ~]$ javac -cp $HADOOP_CLASSPATH:/usr/hdp/2.5.3.0-37/hive2/lib/*  HiveAlterRenameTo.java
HiveAlterRenameTo.java:13: error: unreported exception ClassNotFoundException; must be caught or declared to be thrown
      Class.forName(driverName);
                   ^
1 error
[

Master Collaborator

You need to catch the appropriate exception as the error indicates. Here is an example:

try {
    Class.forName(driverName);
} catch (ClassNotFoundException ex) {
    ex.printStackTrace();
}

Super Mentor
Take a Tour of the Community
Don't have an account?
Your experience may be limited. Sign in to explore more.