Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

hbase java api code runtime error

avatar
Super Collaborator

I am able to compile the code as follows :

 javac -classpath /usr/hdp/2.6.4.0-91/hadoop/client/hadoop-common.jar:/usr/hdp/2.6.4.0-91/hbase/lib/* RetriveData.java
Note: RetriveData.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

but I cant run it , I tried all types of libraries in the classpath

 java -classpath /usr/hdp/2.6.4.0-91/hadoop/client/hadoop-common.jar:/usr/hdp/2.6.4.0-91/hbase/lib/*.jar:. RetriveData
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/hbase/HBaseConfiguration
        at RetriveData.main(RetriveData.java:16)
Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.hbase.HBaseConfiguration
        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)
7 REPLIES 7

avatar
Super Collaborator

as you see I am using the right jar which has the HbaseConfiguration but why its not finding it ?

#java -classpath /usr/hdp/2.6.4.0-91/hadoop/client/hadoop-common.jar:. RetriveData
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/hbase/HBaseConfiguration
  at RetriveData.main(RetriveData.java:16)
Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.hbase.HBaseConfiguration
  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)
  ... 1 more

# jar tvf /usr/hdp/2.6.4.0-91/hbase/lib/hbase-common.jar | grep "org/apache/hadoop/hbase/HBaseConfiguration"
  9678 Thu Jan 04 10:27:46 EST 2018 org/apache/hadoop/hbase/HBaseConfiguration.class

avatar
Super Guru

You have specified an invalid classpath. You should be using:

/usr/hdp/2.6.4.0-91/hbase/lib/*

You can read https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html#A1100762 for a refresher.

avatar
Super Collaborator

if I use that path then I get another error

[root@hadoop1 ~]# java -classpath /usr/hdp/2.6.4.0-91/hbase/lib/*:.  RetriveData
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/conf/Configuration
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:763)

avatar
Super Guru

You didn't need to remove the other valid classpath entry that you had previously...

avatar
Super Collaborator

also this is a centos environment and not windows

avatar
Master Mentor

@Sami Ahmad

Can you please try the following command to setup the classpath. (Please note that in the command i am using Backtick and not the single quote)

Example:
-----------
# javap -classpath `hadoop classpath`:`hbase classpath`:.:  RetriveData
# javap -classpath `hadoop classpath`:`hbase classpath`:.:  com.test.your.packagename.RetriveData

For Testing:
------------
# javap -classpath `hadoop classpath`:`hbase classpath` org.apache.hadoop.hbase.HBaseConfiguration

avatar
Super Collaborator

hi Jay i am not using a package .. how do I compile ?

# more RetriveData.java
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
public class RetriveData{
   public static void main(String[] args) throws IOException, Exception{
      // Instantiating Configuration class
      Configuration config = HBaseConfiguration.create();
      // Instantiating HTable class
      HTable table = new HTable(config, "PUR_ACCT_PHX");
      // Instantiating Get class
      Get g = new Get(Bytes.toBytes("1001181"));
      // Reading the data
      Result result = table.get(g);
      // Reading values from Result class object
      byte [] value = result.getValue(Bytes.toBytes("cf1"),Bytes.toBytes("PUR_ID"));
      byte [] value1 = result.getValue(Bytes.toBytes("cf1"),Bytes.toBytes("PUR_TRANS_DATE"));
      // Printing the values
      String purchaseID = Bytes.toString(value);
      String purchaseDate = Bytes.toString(value1);
      System.out.println("PURCASE ID: " + purchaseID + " PUR_TRANS_DATE: " + purchaseDate);
   }
}