I compiled successfully my program but got a undefined class error when trying to run it. The weird thing is I don't use the cern/colt/matrix/DoubleMatrix1D class in myprogram (I verified this with "grep"). Can someone please point me in the right direction? Thanks
$ 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
NOTE:
DoubleMatrix1D is an abstract class.
I used the subclass 'SparseDoubleMatrix1D' of DoubleMatrix1D in myprogram.
You may not use that class directly / explicitly in the source code of your program. But you definitely do use it ... indirectly.
For example:
It might be the type of an internal field of some class that you are directly using.
It might be a superclass of a some class that you are using directly.
And a few other things ...
Anyhow, the bottom line is that your code won't run unless you put the JAR (or whatever) containing the missing class onto the runtime class path.
Since the missing class is a superclass, the JRE needs it because it needs the code of the constructor for the superclass for when you create an instance of the subclass.
Related
Trying to start java program on linux but it fails to locate JSoup jar file. I have checked other questions and found on how to add libraries while compiling and while trying to run it but I am not sure if I am doing it correctly because it still fails to load the library....
I enter the following commands:
root#vps5441:/var/www/var/www/TimetableLoader# javac -cp /var/www/var/www/TimetableLoader/jsoup-1.8.1.jar:/var/www/var/www/TimetableLoader/mysql-connector-java-5.0.8/mysql-connector-java-5.0.8-bin.jar. TimetableLoader.java
root#vps5441:/var/www/var/www/TimetableLoader# java -cp /var/www/var/www/TimetableLoader/jsoup-1.8.1.jar:/var/www/var/www/TimetableLoader/mysql-connector-java-5.0.8/mysql-connector-java-5.0.8-bin.jar. -classpath /var/www/var/www/TimetableLoader/ TimetableLoader
And I get the following error when the program tries to access the JSoup library.
Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/Jsoup
at TimetableLoader.main(TimetableLoader.java:46)
Caused by: java.lang.ClassNotFoundException: org.jsoup.Jsoup
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)
... 1 more
I assume jsoup-1.8.1.jar is in the current directory?
You don't generally need both -classpath and -cp. It's probably ignoring the values given by -cp as a result.
Put your own classes ahead of the 3rd-party ones.
You're already in /var/www/var/www/TimetableLoader so keep it simple:-
root#vps5441:/var/www/var/www/TimetableLoader# java -cp .:jsoup-1.8.1.jar:mysql-connector-java-5.0.8/mysql-connector-java-5.0.8-bin.jar TimetableLoader
(Best to get out of the habit of running things as "root" unless you have a genuine need, just sayin')
I tried to execute CreateTextFileTest.class file with classpath in the terminal as the following:
java -classpath ..:"/home/fatih/NetBeansProjects/Unit17 - CreatingTextFile/src/unit17/unit17/creatingtextfile" CreateTextFileTest
My class files in this directory : /home/fatih/NetBeansProjects/Unit17 - CreatingTextFile/src/unit17/unit17/creatingtextfile .
I have 3 classes in the directory: AccountRecord.class , CreateTextFile.class, and CreateTextFileTest.class
But, when I executed the CreateTextFileTest from the terminal with the code above, an error occured like that:
Exception in thread "main" java.lang.NoClassDefFoundError: CreateTextFileTest (**wrong name**: unit17/creatingtextfile/CreateTextFileTest)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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 sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
As far as I understand because CreateTextFileTest.java uses two different classes, executing fails. How to handle this situation? How to run my java project? How to use classpath in this situation? Am I using wrongly?
The error message states that the package name declared in your compiled class doesn't match what the JVM was expecting given your classpath setup. The classpath should point to the directory from which your packages start, not the directory actually containing your .class files. Given your error message I believe that this should work:
java -classpath ..:"/home/fatih/NetBeansProjects/Unit17 - CreatingTextFile/src/unit17" unit17.creatingtextfile.CreateTextFileTest
Given the duplicate unit17 in your path I get the impression there's something else mixed up here. You'll get a better answer on Stack Overflow...
I'd like to run WsImport directly with the java command. I'm using the following command (with CLASSPATH environment set up):
$ java com.sun.tools.ws.WsImport -d /home/bence/NetBeansProjects/WebFormsTest/build/generated-sources/jax-ws -Xendorsed -keep -B-jaxb-facets -wsdl -r /home/bence/NetBeansProjects/WebFormsTest/build/generated-sources/jax-ws/resources -s /home/bence/NetBeansProjects/WebFormsTest/build/generated-sources/jax-ws -verbose org.czentral.test.service.AdminService
And get the error message in the title. With all the details:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/tools/xjc/Plugin
at java.lang.ClassLoader.findBootstrapClass(Native Method)
at java.lang.ClassLoader.findBootstrapClassOrNull(ClassLoader.java:1058)
at java.lang.ClassLoader.loadClass(ClassLoader.java:413)
at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
at com.sun.istack.tools.MaskingClassLoader.loadClass(MaskingClassLoader.java:82)
at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:270)
at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:363)
at java.util.ServiceLoader$1.next(ServiceLoader.java:445)
at com.sun.tools.xjc.Options.findServices(Options.java:952)
at com.sun.tools.xjc.Options.getAllPlugins(Options.java:374)
at com.sun.tools.xjc.Options.parseArgument(Options.java:692)
at com.sun.tools.ws.wscompile.WsimportOptions.parseArguments(WsimportOptions.java:326)
at com.sun.tools.ws.wscompile.WsimportOptions.parseArguments(WsimportOptions.java:232)
at com.sun.tools.ws.wscompile.WsimportTool.parseArguments(WsimportTool.java:359)
at com.sun.tools.ws.wscompile.WsimportTool.run(WsimportTool.java:193)
at com.sun.tools.ws.wscompile.WsimportTool.run(WsimportTool.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.sun.tools.ws.Invoker.invoke(Invoker.java:135)
at com.sun.tools.ws.WsImport.main(WsImport.java:57)
Ok, this far a reasonable explanation would be a missing .jar from classpath. But I actually have the right jar, which is confirmed with the following:
$ java com.sun.tools.xjc.Plugin
Error: Main method not found in class com.sun.tools.xjc.Plugin, please define the main method as:
public static void main(String[] args)
So the classloader actually can load the class.
What condition can result in an error like this?
This is a platform specific issue. Code was compiled in a platform resulting in a platform specific jar, and then, although the class and the jar exist in your classpath, java does not recognize it.
NoClassDefFoundError in Java comes when Java Virtual Machine is not able to find a particular class at runtime which was available during compile time.
Read more at: http://javarevisited.blogspot.com/2011/06/noclassdeffounderror-exception-in.html#ixzz2ILpyqUVe
I'm writing a program in Eclipse, and running it from the command line. In an earlier version of the program, it took no arguements, and I could run it fine as > java foo. I've since added a couple of arguments, and need to run it as > java foo file1.txt file2.txt. When I run this, I get a java.lang.NoClassDefFoundError: error. Even when I include the classpath, i.e. > java foo file1.txt file2.txt -cp . it still doesn't work.
Could someone point me in the right direction?
EDIT
Here is the complete stack trace
Exception in thread "main" java.lang.NoClassDefFoundError: edu/cuny/pausePred/TemplateToCharTestVector (wrong name: edu/cuny/pausepred/TemplateToCharTestVector)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:480)
Write the full directory path then compile all classes
Write the full directory and run the class that contains the main() method
A common mistake for beginners in using Java is misunderstanding class names and the classpath.
A class name is a fully qualified thing which includes the package; the compiler lets you refer to a class using its base name, which is a convenience to keep programming sane. The actual name of your class is <package>.foo.
The classpath must include the root of any packages which you are using. So if your package for foo is edu.cuny.pausePred then the class name for foo is edu.cuny.pausePred.foo and the classpath must include the directory which contains edu, not the directory which contains foo.
You command line should be something like:
jave -cp the-directory-root-for-java-sources foo file1.txt file2.txt
noting that this assumes that the two data files are in the current directory.
As an aside, note that a class base name should be lead uppercase, so Foo, not foo.
Exception in thread "main" java.lang.NoClassDefFoundError:
edu/cuny/pausePred/TemplateToCharTestVector
(wrong name: edu/cuny/pausepred/TemplateToCharTestVector)
Paths in java are case-sensitive. pausepred is not the same as pausePred
I have no experience with Scala, so this question may be elementary. I am trying to use Scala class from within Java, based on the "Person" example in this tutorial: http://www.codecommit.com/blog/java/interop-between-java-and-scala
I create two source files, one Scala and one Java, as follows.
Person.scala:
class Person {
def getName() = "Daniel Spiewak"
}
Test.java:
public class Test {
public static void main(String[] args) {
Person p = new Person();
p.getName();
}
}
I can compile (with a warning I don't understand), but I get a ClassNotFoundException when I try to run the program.
$ scalac Person.scala
$ javac Test.java
./Person.class: warning: Cannot find annotation method 'bytes()' in type 'ScalaSignature': class file for scala.reflect.ScalaSignature not found
1 warning
$ java Test
Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:787)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:447)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at Test.main(Test.java:3)
Caused by: java.lang.ClassNotFoundException: scala.ScalaObject
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 13 more
Any idea what I'm doing wrong?
You need to include the Scala runtime library to be able to use Scala classes. This is the cause of both the compiler warning and the error at runtime.
Try this
java -cp .:scala-library.jar Test
(the name of the jar file might be different, but the point is to add it to your classpath).
Did you put scala-library.jar in your classpath? Seems you forgot this, so JVM cannot find some internal class for Scala runtime.
In your link http://www.codecommit.com/blog/java/interop-between-java-and-scala:
Just stick scala-library.jar on your classpath and all of your Scala
classes should be readily available within your Java application.