2015-02-07 123 views
-2

我编译成功了我的程序,但在运行它时遇到了未定义的类错误。奇怪的是我没有在myprogram中使用cern/colt/matrix/DoubleMatrix1D类(我用“grep”验证了这一点)。有人可以请指点我正确的方向吗?由于为什么java会抱怨一个没有使用的类?

$ javac -cp $(find ../resources/ -name "*.jar"|tr "\n" ":") myprogram.java 
Note: Some input files use unchecked or unsafe operations. 
Note: Recompile with -Xlint:unchecked for details. 

$ java myprogram 
Exception in thread "main" java.lang.NoClassDefFoundError: cern/colt/matrix/DoubleMatrix1D 
     at java.lang.Class.getDeclaredMethods0(Native Method) 
     at java.lang.Class.privateGetDeclaredMethods(Class.java:2615) 
     at java.lang.Class.getMethod0(Class.java:2856) 
     at java.lang.Class.getMethod(Class.java:1668) 
     at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494) 
     at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486) 
Caused by: java.lang.ClassNotFoundException: cern.colt.matrix.DoubleMatrix1D 
     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) 
     ... 6 more 

注:

  1. DoubleMatrix1D是一个抽象类。
  2. 我在myprogram中使用了DoubleMatrix1D的子类'SparseDoubleMatrix1D'。
+1

很明显你会在你的程序中使用它。请注意,当您编译程序时(编译器知道它们),您会在类路径中添加一堆jar文件,但在运行程序时不会。 – immibis 2015-02-07 23:26:29

回答

0

您不能在程序的源代码中直接/明确地使用该类。但你绝对间接使用它...。

例如:

  • 这可能是你直接使用一些类的内部字段的类型。

  • 它可能是您直接使用某个类的超类。

  • 和一些其他的东西......


无论如何,底线是,你的代码将不会运行除非你把JAR(或其他)包含丢失类到运行时类路径上。

由于缺少的类是超类,JRE需要它,因为它在创建子类的实例时需要超类构造函数的代码。

+0

非常感谢。这工作! – cody 2015-02-07 23:38:48

相关问题