user1391967
user1391967

Reputation: 497

Charts4j library working on Java Project but not on Android projet?

I'm using this library : CHARTS4J VERSION 1.3

This demo class : See my class is working on a java project (under eclipse). But in my android project, i have this error : "java.lang.NoClassDefFoundError: com.googlecode.charts4j.Data" for the very first object my class is trying to use (this line : Plot plot = Plots.newPlot(Data.newData(0, 66.6, 33.3, 100));)

I did have the library in the "build path" of my project and checked that it was in my classpath :

classpathentry kind="lib" path="D:/WorkspaceAndroid/myproject/charts4j-1.3.jar"

I'm lost. Why it's not working at runtime ??

Tks for you help.

Upvotes: 1

Views: 822

Answers (2)

jorg.mer
jorg.mer

Reputation: 155

Same problem here, but building an Eclipse plugin.

What is reason of NoClassDefFoundError in Java?

NoClassDefFoundError in Java comes when Java Virtual Machine is not able to find a particular class at runtime which was available during 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. It’s important to understand that this is different than ClassNotFoundException which comes while trying to load a class at run-time only and name was provided during runtime not on compile time. Many Java developer mingle this two Error and gets confused.

In short NoClassDefFoundError will come if a class was present during compile time but not available in java classpath during runtime.

Difference between java.lang.NoClassDefFoundError and ClassNotFoundException in Java

Many a times we confused ourselves with java.lang.ClassNotFoundException and java.lang.NoClassDefFoundError, though both of them related to Java Classpath they are completely different to each other. ClassNotFoundException comes when JVM tries to load a class at runtime dynamically means you give the name of class at runtime and then JVM tries to load it and if that class is not found in classpath it throws java.lang.ClassNotFoundException. While in case of NoClassDefFoundError the problematic class was present during Compile time and that's why program was successfully compile but not available during runtime by any reason. NoClassDefFoundError is easier to solve than ClassNotFoundException in my opinion because here we know that Class was present during build time but it totally depends upon environment, if you are working in J2EE environment than you can get NoClassDefFoundError even if class is present because it may not be visible to corresponding ClassLoader.

For more details: http://javarevisited.blogspot.com.es/2011/06/noclassdeffounderror-exception-in.html

Now the SOLUTION for Eclipse Plugin

When we get a NoClassDefFoundError the classpath is not defined in at least one place. In an Eclipse plugin these are all the places where it is necessary to define the classpath of an external library:

  1. Project Properties: Right click on the project, then properties. Then Java Build Path, then Libraries. And finally add the jars of the libraries (externals or in a project's folder)
  2. In the plugin.xml file: Open the plugin.xml file, and click in the tab Runtime. Add the libraries in the Classpath section. The click in the tab Build and add the libraries in the Extra Classpath Entries.

And thats all.

For an Android project should be a similar solution since this kind of error come from bad classpath definitions.

Upvotes: 0

Tschegewara
Tschegewara

Reputation: 169

I do not really understand what problem you have, with chart4j. But I also tried charts on Android, and androidplot.com is an easy to use well documented library, that gave me no headaches while programming.

Upvotes: 1

Related Questions