How do I get started with JFreeChart?

纵饮孤独 提交于 2019-12-23 03:35:35

问题


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 (e.g. 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

回答1:


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


来源:https://stackoverflow.com/questions/16624671/how-do-i-get-started-with-jfreechart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!