Why Exception in thread "main" java.lang.NoClassDefFoundError:? - java

I run my software through Eclipse. Yesterday everything was fine. I made not changes to the code but today, when I am trying to run it again I get the following error messages:
Exception in thread "main" java.lang.NoClassDefFoundError: coloredtrails/CTListener
at test.DemoPlayer1.createAndShowGUI(DemoPlayer1.java:23)
at test.DemoPlayer1.main(DemoPlayer1.java:39)
Caused by: java.lang.ClassNotFoundException: coloredtrails.CTListener
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more
Why it does not see the class? What could be the reason of that? How can I resolve the problem?

A NoClassDefFoundError (almost) always means that your classpath is wrong. Make sure that your classpath includes the base directory of the coloredtrails package. (Ofcourse, also make sure that the file coloredtrails\CTListener.class actually exists).
When running from the command line:
You can set the classpath by setting the CLASSPATH environment variable, or by specifying it with the -cp or -classpath option on the command line when you run your program. For example:
java -cp C:\MyProject\classes coloredtrails.CTListener
edit - Looking at the stack trace and seeing URLClassLoader in there makes me think that you are trying to run a Java applet. To learn how to correctly deploy applets, so that all classes the applet needs can be found, see this tutorial: Deploying an Applet.

Sometimes, my Eclipse (Indigo on MacOSX) does that, expecially if I do changes (removing files, moving them around) to the project structure on the filsystem directly.
Basically, eclipse cannot find the source folder anymore, so he doesn't compile the source but tries to run it anyway (all this without a warning or a reference to the problem).
To fix it, remove the source folder from the build path (=right click on the src folder under the project in the package explorer, then choose "Build-path->Remove from Build-path". Then, add it again (=right click on the folder under the project in the package explorer and choose "Add to build-path"). This makes the src folder "visible" to the compiler again and fixes the problem.

Related

Application runs fine in Intellij but doesn't work when packaged into an exe file

I have built a very simple command line application using picocli and I would like to package it into an executable file. I first packaged it into a jar file using Maven package, and then used Launch4J as recommended by several users on SO to obtain an executable. However, it throws the NoClassDefFound error, when I run Application.exe in the command prompt.
> Exception in thread "main" java.lang.NoClassDefFoundError:
> picocli/CommandLine
> at mycli.HelloCli.main(HelloCli.java:48)
> Caused by: java.lang.ClassNotFoundException: picocli.CommandLine
> at java.net.URLClassLoader.findClass(Unknown Source)
> at java.lang.ClassLoader.loadClass(Unknown Source)
> at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
> at java.lang.ClassLoader.loadClass(Unknown Source)
> ... 1 more
The application works perfectly well when executed in IntelliJ. I tried adding the custom jar file I created by looking at answers similar to these but it doesn't seem to help. I also tried changing the folder of execution but that doesn't solve the error either.
From several answers I searched across SO, I realized that the picocli jar needed to be on the class path. So I added the "Main class" as the one containing my main method, and specified the "Classpath" to the jar file created by building an artifact out of all the dependencies present, while configuring Launch4J.
For details on how to build an artifact jar out of a project's dependencies, visit this answer if you use Eclipse, or this answer if you use IntelliJ.)

using eclipse, java maven project compiles but gives error when running

There are a large number of answers to the error I'm getting, but each of the solutions that I've tried (that I understand at least) have not solved my problem.
My project layout in Eclipse looks like this:
MavenProject1
src
resources
etc.
MavenProject2
src
resources
etc.
MavenProjectX
src
resources
etc.
Each project compiles and runs fine. But what I'm trying to do is use one of the projects, say some of the code in MavenProject2, in MavenProject1. I've updated the build path and information so that I can import MavenProject2 into MavenProject1 and reference the methods I want.
The reference to the MavenProject2 method in MavenProject1 enumerates and doesn't give an error in the editor (i.e. com.MavenProject2.Method()), and MavenProject1 compiles. But when I try to run it, I get the error below. I've tried fixing the classpath (as I understand it, which could be wrong), I've tried adding the project, external jar, just about every option, in the run configuration for the project, but I keep hitting the same error on line in MavenProject1 that calls the method in MavenProject2.
[WARNING] java.lang.reflect.InvocationTargetException at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
> sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at
> java.lang.reflect.Method.invoke(Unknown Source) at
> org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297) at
> java.lang.Thread.run(Unknown Source) Caused by:
> java.lang.NoClassDefFoundError: com/MavenProject2/Method_From2 at
> com.MP1.MP1.main(CAPI.java:154) ... 6 more Caused by:
> java.lang.ClassNotFoundException: com.MavenProject2.Method_From2
When you first build the projects that your current project depends on, make sure they are stored in your local maven repository. Then when you add those as dependencies to your project's pom.xml this should hopefully resolve it for the build.

How to use xstream fro xml serialization in eclipse

I am having some trouble figuring out how to download and use xstream in eclipse. When I download the binary distribution from the website, I get a zip file. Where should I extract it, and how do I use it?
Edit:
OK, I have added it to my build path, but now when I try to use XStream to serialize an ArrayList<Earmark> where Earmark is a class that I have defined, I get the following error message:
Exception in thread "main" java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserException
at com.thoughtworks.xstream.XStream.<init>(XStream.java:334)
at examples.TestEarmarkExtractor.SerializeEarmarks(TestEarmarkExtractor.java:19)
at examples.TestEarmarkExtractor.main(TestEarmarkExtractor.java:48)
Caused by: java.lang.ClassNotFoundException: org.xmlpull.v1.XmlPullParserException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more
Update: The error is occurring before I try the serialization. It occurs on the line
XStream xs = new XStream();
Extract the zip file to a directory (e.g. C:\Program Files\XStream)
In eclipse, right-click on your project and select Build Path > Configure Build Path...
Select Libraries tab
Select Add External Jars..
Navigate to the lib folder of your XStream download and select the jar(s) to be added
Select Open
Select OK
And here's the Two Minute Tutorial on how to use XStream.
Got this exception, then I download kxml2-min-2.3.0.jar and add it as a library and now it works (using xstream 1.4.4.jar)
You need to unzip the .zip file to get at the .jar file, which you put on your classpath for your project in eclipse (Google if you don't know how to add third party library .jars to your project classpath in Eclipse).
Once it is added to your project's classpath, you use the classes contained within it as you would any other classes, and the documentation on the XStream website tells you how to use them.
Good choice by the way, XStream is great!
You can add this library:
<dependency>
<groupId>xmlpull</groupId>
<artifactId>xmlpull</artifactId>
<version>1.1.3.1</version>
</dependency>
I had added the two libraries: dom4j-2.0.0-ALPHA-2.jar and xstream-1.4.7.jar and the same issue were displayed. After adding the kxml2-min-2.3.0.jar the issue is not happening.

problems using PMD in IntelliJ IDEA

I was trying to install the PMD plugin to my IntelliJ IDE. I have followed the instructions but when trying to run the pmd check I am getting the following exception. From what it seems the “.” Is being parsed as a / which produce a wrong file name for the class loader.
Meaning the path: Even though the settings are
-cp %CLASSPATH%; C:/Development/IDE/Utils/PMDPlugin/lib/pmd-4.2.5.jar; C:\Development\IDE\Utils\PMDPlugin\lib\asm-3.1.jar; C:\Development\IDE\Utils\PMDPlugin\lib\jaxen-1.1.1.jar net.sourceforge.pmd.PMD "$FilePath$" ideaj unusedcode,imports "$Sourcepath$" $FileClass$.method $FileName$
From some reason it is still looking for C:/Development/IDE/Utils/PMDPlugin/lib/pmd-4/2/5/jar
Here is the exception:
java.lang.NoClassDefFoundError: C:/Development/IDE/Utils/PMDPlugin/lib/pmd-4/2/5/jar;
Caused by: java.lang.ClassNotFoundException: C:.Development.IDE.Utils.PMDPlugin.lib.pmd-4.2.5.jar;
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: C:/Development/IDE/Utils/PMDPlugin/lib/pmd-4.2.5.jar;. Program will exit.
Exception in thread "main"
I am using Intelli IDEA 10.0.3
Build #IC 103.288 built on April 2011
JDK 1.6.0_21
Do you have any advices?? Thanks
Looks like you have spaces between your CLASSPATH entries. That won't do; remove them.
UPDATE:
I downloaded PMD and managed to make it run. I followed the directions that you posted, replacing the path to PMD with my own. I'd recommend not using a path that has spaces in it.
The one thing that was balky was %JAVA_HOME%, so I replaced it with the hard-wired path to my Java /bin. Once I did that, all was well.
I'd ask why you think PMD is better than the inspector tools built into IntelliJ? I'd rather check boxes to set rules than have to do all the nonsense that PMD requires.
Once I was satisfied that I could make it work I deleted it. I'd rather run Inspector.

Getting a weird ClassNotDefFoundException on runtime with GWT

I am deploying a web application based on GWT. When I compile, I don't get any errors, but in runtime, I get this one (not complete stack trace):
java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserException
at org.gvsig.remoteClient.sos.SOSClient.<init>(SOSClient.java:47)
at com.uji.project.server.ConnectServiceImpl.makeConnection(ConnectServiceImpl.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Caused by:
Caused by: java.lang.ClassNotFoundException: org.xmlpull.v1.XmlPullParserException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
I have read on this website that that is probably caused if one or more classes are not available on running time but, as seen, the error is caused by some problem with the XML parser, but I have the XML parser included in my lib folder. In detail, in the project's lib folder, it is possible to find 3 JAR files: gwt-maps.jar, gwt-servlet.jar, and kxml2.jar*.
If I open "kxml2.jar" file I can see folder org that within has xmlpull folder, that within has v1 folder and finally XmlPullParserException.class.
So...why on runtime do I not have this class available if it is included on the lib folder?
In appengine classes can only access to the classesof the JRE which are in this whitelist. If this jar is using another class of the JRE it won't be included in runtime.
Here you can read that "XML processing APIs including DOM, SAX, and XSLT" are compatible with appengine. Maybe xmlpullparser is not compatible, but I'm not sure.
Finally a colleague told me the solution to this runtime error! :D
When a GWT application is deployed, many files are copied under a small file structure (pending from ) that contains all classes, libraries and other files that are needed on runtime.
For some reason, if you don't tell on purpose Eclipse to copy (on compile/deploy time) a specific library into that PATH, the application will not find on runtime that library and will raise an error.
Solution (for me) was as simple as copying by hand that library into that folder: In my case was copying "kxml2.jar" under "C:\gwt-2.1.0\SOSProject\war\WEB-INF\lib". I restarted the application and just worked. :D
I hope this patch/solution will help others to solve this annoying problem under GWT!
Try this
Window->Preferences->Java Build Path->Add Jars
Navigate to your project/lib, select kxml2.jar Hit OK.
In the end, what I did was to start again from the beginning: In the same workspace I had different projects and maybe there were, let's say, interferences between them.
So, I unzipped again the GWT SDK, start a new project, copied the original source code on the new project, deployed it and on runtime, now I am not getting that weird ClassNotDefFound error. Now I am getting another one, but that is another issue! Thanks to all.

Categories