java 类路径中的外部 jars 和 HBase
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3502332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
External jars & HBase in classpath
提问by wlk
I'm having problem with using external jars in my file. I always get:
我在文件中使用外部 jar 时遇到问题。我总是得到:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/hbase/HBaseConfiguration`
at this line: `Configuration config = HBaseConfiguration.create();
And this file is on the classpath, I'm setting options when running jar:
这个文件在类路径上,我在运行 jar 时设置选项:
$ java -jar hbase.jar -classpath "/usr/lib/hbase/*:/usr/lib/hadoop/*"
This file compiles successfully after invoking this command:
调用此命令后,此文件编译成功:
$ javac -classpath "/usr/lib/hbase/*:/usr/lib/hadoop/*" HBaseScanner.java
What to do?
该怎么办?
采纳答案by wlk
My problem was that I was setting classpath using -classpathoption and running jar, which causes the -classpathoption to be ignored.
我的问题是我正在使用-classpath选项设置类路径并运行 jar,这会导致该-classpath选项被忽略。
I have added jars in manifest file and this works.
我在清单文件中添加了 jars,这是有效的。
This is similar problem: Setting multiple jars in java classpath
回答by hidralisk
JVM will throw java.lang.NoClassDefFoundError if the class loader cannot initialize static members of a class, say an exception is thrown. Or if it cannot find another class that is accessed by a static initializer in this class. This looks like what is happening, when class loader tires to load HBaseConfiguraton, this class HBaseConfiguration is expecting something that it cannon get that leads to an exception, that is lost.
Tough luck debugging this sort of failures.
EDIT:
The easiest way to figure out what is missing is to launch the program in a debugger with all HBase sources connected, say under Eclipse, and trace the HBaseConfiguration.create()call.
如果类加载器无法初始化类的静态成员,例如抛出异常,JVM 将抛出 java.lang.NoClassDefFoundError。或者,如果它无法找到该类中的静态初始值设定项访问的另一个类。这看起来像正在发生的事情,当类加载器厌倦加载 HBaseConfiguraton 时,此类 HBaseConfiguration 期望它无法获得的东西会导致异常,即丢失。
调试这种故障真是太倒霉了。
编辑:找出缺少什么的最简单方法是在连接所有 HBase 源的调试器中启动程序,例如在 Eclipse 下,并跟踪HBaseConfiguration.create()调用。
回答by MikeKulls
It would be really handy if java gave us an error or at least a warning in this situation. I believe one solution is to execute your jar like this
如果在这种情况下 java 给我们一个错误或至少一个警告,那将非常方便。我相信一种解决方案是像这样执行你的 jar
java -cp "myclasspath":myjarfile.jar MyClassname

