简体   繁体   中英

How do I get started with JFreeChart?

I've never used any third party library before. What should I do after I downloaded jfreechart-1.0.14.tar.gz ?

I don't know if I'm doing these things right:
1. Put the jcommon-1.0.17.jar and jfreechart-1.0.14.jar at the same directory as my source code.
2. Import needed class in the source code (eg import org.jfree.util.Rotation; )

Many articles tell you how to do this in IDEs. But instead of IDEs, I'm writing codes with vim and compile by myself. So, assume I didn't do any thing wrong, how should I compile the source code with javac and run the code with java ?


Edit:

Here's my file layout:
./src
| - test.java
./lib
| - jcommon-1.0.17.jar
| - jfreechart-1.0.14.jar

I compile by
javac -cp "lib/*" -d classes/ src/test.java
then run by
java -cp classes:lib/jcommon-1.0.17.jar:jfreechart-1.0.14.jar test

However, some error occurs:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jfree/data/general/PieDataset

How can I resolve this problem?


Exception in thread "main" java.lang.NoClassDefFoundError: org/jfree/data/general/PieDataset  
at java.lang.Class.getDeclaredMethods0(Native Method)  
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)  
at java.lang.Class.getMethod0(Unknown Source)  
at java.lang.Class.getMethod(Unknown Source)  
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)  
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)  
Caused by: java.lang.ClassNotFoundException: org.jfree.data.general.PieDataset  
at java.net.URLClassLoader$1.run(Unknown Source)  
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)  
... 6 more

The libraries shouldn't be at the same place as the source code. If you don't want to use a build tool like Gradle yet, which would handle your library dependencies, then I suggest using the following layout:

project
    src
        .java files here, organized in a folder tree matching the package tree
    classes
        compiled .class files here
    lib
        .jar files here

To compile, go in the project directory and execute the following command:

javac -cp lib/jfreechart-1.0.14.jar:lib/jcommon-1.0.17.jar -d classes src/com/foo/bar/MyClass.java src/com/foo/bar/MyOtherClass.java

To run your app, execute the following command:

java -cp classes:lib/jfreechart-1.0.14.jar:lib/jcommon-1.0.17.jar com.foo.bar.MyClass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM