I'm trying to run a Java program and somewhere along the execution, I get a
java.lang.NoClassDefFoundError: antlr/TokenStream
exception. I'm new to java programming so don't really know what this means. I've looked through some other questions about the same issues and they didn't really help me out - either I couldn't follow the answer or it didn't apply in my case.
Any ideas?
search for antlr.jar and place it to your classpath
java.lang.NoClassDefFoundError is thrown when a particular class referenced by your program is not available in the classpath. Classpath is the list of paths/directories that the runtime searches for the classes used in the class being run.
The error message you get means that antlr/TokenStream is not available in your classpath.
To include the corresponding jar (antlr.jar) to the classpath, you ca use the -cp flag while running:
java -cp .;path_to_antlr.jar yourClass
Or
java -cp .;path_to_antlr.jar -jar yourJar.jar
It is searching for the defn of the class, which it is not finding in the classpath.
From the JavaDoc's,
Thrown if the Java Virtual Machine or a ClassLoader instance tries to
load in the definition of a class (as part of a normal method call or
as part of creating a new instance using the new expression) and no
definition of the class could be found.
Thrown if the Java Virtual Machine or a ClassLoader instance tries to
load in the definition of a class (as part of a normal method call or
as part of creating a new instance using the new expression) and no
definition of the class could be found.
The searched-for class definition existed when the currently executing
class was compiled, but the definition can no longer be found.
Take from here.
You have made reference to a java class but for some reason, that class does not exist in the execution image you are running. For example, if you instantiated a new class XYZ like:
XYZ xyz = new XYZ (), but no such class existed you would get an error similar to the above. In the past, I've received this kind of error if I misspelled a reference to a class or more typically, if the class to which I was referring somehow did not get included in my jar. Check the jar or the directory in which you are doing the execution. Do you see the class to which you are making reference there? I bet it is missing.
Elliott
Related
I have read a lot of answers here to NoClassDefFoundError, but somehow I could not find one that solves my problem. Maybe I have not enough experience to transform it to my problem.
Anyway, I have a project (imported over git) with a package and lots of helpful functions. There is as well a package with some demos, they work completely fine.
If I now copy one of those demos in my own project, I get the error
Exception in thread "main" java.lang.NoClassDefFoundError: org/jfree/data/xy/XYDataset
at net.[...]
I think there must be a simple solution, since it runs in the other project.
Thank you for taking your time.
NoClassDefFoundError in Java comes when Java Virtual Machine is not able to find a particular class at runtime which was available at compile time. For example, if we have a method call from a class or accessing any static member of a Class and that class is not available during run-time then JVM will throw NoClassDefFoundError.
The class is not available in Java Classpath.
You might be running your program using jar command and the class was not
defined in manifest file's ClassPath attribute.
Any start-up script is overriding Classpath environment variable.
Because NoClassDefFoundError is a subclass of java.lang.LinkageError
it can also come if one of its dependency like native library may not available.
If you are working in J2EE environment than the visibility of Class
among multiple Classloader can also cause
java.lang.NoClassDefFoundError, see examples and scenario section for detailed discussion.
Maybe this question may be splitting hairs, but when I compile a file from command line like :
javac MyClass.java
then afterward I cannot run it by saying
java MyClass.class
I have to call:
java MyClass
What is the motivation for this notation?
Because you run a class on the classpath, which may be contained inside a jar for example. You couldn't use your syntax in that case.
Java compiler needs a compilation unit; this is by default (at least) a java source file, with the whole of classes defined in it and its dependencies.
Java interpreter (the jvm) needs a single class with a main method as entry point of the execution - it must start somewhere.
You'd have to ask Sun (now Oracle) for the development history, but I do want to point out that for folks who are just using Java rather than developing Java, "java DoSomething" is easier to remember, and to type, than "java DoSomething.class"
There is no way to run a Java program that is not a class. For that reason, there is no reason to mandate typing the ".class". You might also invoke a class from within a JAR on your path, or directly, but it's still instantiating a class (possibly a "default" class from the Manifest).
Because the name of the class is MyClass and not MyClass.class. And when running java you specify the CLASS NAME and not the PATH to the actual compiled file.
For more in depth knowledge I guess Sun & Oracle will have to answer :)
Imagine that you have a class named package and you have a class named Class, in a package named package,
--CurrentFolder
--package
Class.class
package.class
so executing java package.class may lead to an undecidability to the compiler!
java.lang.NoClassDefFoundError: Could not initialize class org.apache.axis.utils.XMLUtils
I am getting this error. How to avoid this exception. Please suggest me a solution.
According to Oracle:
NoClassDefFoundError is thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.
In simple language, it means "At compile-time class was there but at run-time, it failed to find/load the class
Question comes : How come my code compile?
Answer maybe because you added jars using Eclipse. But Eclipse does not actually move those jars into you classpath.It just uses those referenced jars while compilation. So your code compiles fine.
After, you move your project to tomcat, when it tries to load some class inside those 'jars', it fails to find the class,because you never moved those jars to the classpath.
Solution:
Move all the libraries(jars) into your project's /WEB-INF/lib. Now all the libraries/jars under /WEB-INF/lib will come under classpath.
You can read more on Oracle's Docs & this article
NoClassDefFoundError means, a library that was available at the compile time, is not available at the runtime.
In this case it's the jar file containing the class org.apache.axis.utils.XMLUtils. Make sure it is available in your classpath.
This exception probably means that an instance has to be created by reflection, but the corresponding class is not in the execution classpath. Check your execution classpath.
It is also possible that the creation of the new instance depends on some configuration that is wrong or absent. Check your execution configuration files.
NoClassDefFoundError means the compiler trys to load the class at compile time but the required class is not available at compile time.
So add the required jar file to your program. Can you add this jar file: axis-1.2.jar
I have several jars containing the exact same class.
Lets say for example, A.jar and B.jar have the same class Hello.class. What will happen if I run the following:
java -classpath A.jar;B.jar com.testing.testcode
My question is whether it will run or not? As from my understanding class loader will scan the classpath and it will return whatever Hello.class it finds first.
I know I can avoid this problem if I use OSGi for loading a particular class.
But what my question is – whether this will run or not? Or the java -classpath A.jar;B.jar com.testing.testcode will break down for having two version of the same class.
This will work and the first Hello class in the classpath will be used, so in this case, the one from the A.jar
The class path is a "path". The first match is taken.
There is a boot class path which is examined first and this included the JARs in the JRE. BTW You can prepend these and override system classes but you are not supposed to and it might not be allowed in your license agreement.
Whenever you include the jar in your classpath, on startup/ on application class gets loaded into the container.
Now whether your code will work or not will depend on which jar gets loaded first in your container and accordingly if you are lucky then in your current setup it will work else you might get some error saying that method1 doesn't accept Type1 it should accept Type2.
I am trying to run a java program on my AIX server. I wrote the classpath
/usr/java6/bin/javac -classpath :.:/usr/jdk/commons-net-3.0.1.jar:/usr/jdk/classes12.jar:/usr/jdk/mysql-connector-java-5.1.17-bin.jar:/usr/jdk/jtds-1.2.5.jar:/urs/jdk/mail.jar:/usr/jdk/joda-time-1.6.2.jar:/usr/jdk/commons-codec-1.4.jar:/usr/jdk/commons-logging-1.1.1.jar:/usr/jdk/httpclient-4.1.1.jar:/usr/jdk/httpclient-cache-4.1.1.jar:/usr/jdk/httpcore-4.1.jar:/usr/jdk/httpmime-4.1.1.jar:/usr/jdk/mailapi.jar:/usr/jdk/pop3.jar:/usr/jdk/smtp.jar:/usr/jdk/dsn.jar:/usr/jdk/imap.jar -d . *.java
which seems to compile correctly. Then when I run the program
java daily_transmission
i get the error java.lang.NoClassDefFoundError: javax.mail.Address. All the .jar files that were in Eclipse when it worked before are in the /usr/jdk file.
A NoClassDefFoundError means that (quoting the relevant JavaDoc):
Thrown if the Java Virtual Machine or a ClassLoader instance tries to
load in the definition of a class (as part of a normal method call or
as part of creating a new instance using the new expression) and no
definition of the class could be found.
The searched-for class definition existed when the currently executing
class was compiled, but the definition can no longer be found.
Two things to check:
Firstly, you appear to have have a typo in your classpath (though it can't have been there when you compiled the code, or it wouldn't have compiled successfully, so that's clearly not the root cause of the exception you're seeing.):
/urs/jdk/mail.jar
should be
/usr/jdk/mail.jar
Make sure that you are also using the -classpath option to pass the classpath to java as well as to javac to pick up those .jar files at runtime:
java -classpath :.:/usr/jdk/commons-net-3.0.1.jar:/usr/jdk/classes12.jar:/usr/jdk/mysql-connector-java-5.1.17-bin.jar:/usr/jdk/jtds-1.2.5.jar:/urs/jdk/mail.jar:/usr/jdk/joda-time-1.6.2.jar:/usr/jdk/commons-codec-1.4.jar:/usr/jdk/commons-logging-1.1.1.jar:/usr/jdk/httpclient-4.1.1.jar:/usr/jdk/httpclient-cache-4.1.1.jar:/usr/jdk/httpcore-4.1.jar:/usr/jdk/httpmime-4.1.1.jar:/usr/jdk/mailapi.jar:/usr/jdk/pop3.jar:/usr/jdk/smtp.jar:/usr/jdk/dsn.jar:/usr/jdk/imap.jar daily_transmission
NoClassDefFoundError means that the ClassLoader was able to find the class, but it was not able to be loaded. This usually happens when some static field in the offending class could not be initialized. So I am sure that your classpath might be correct; but some static field in that class could not be set correctly.