如何将JFreeChart库添加到JDK?错误:软件包org.jfree.chart不存在 [英] How to add JFreeChart library to JDK? Error: package org.jfree.chart does not exist
问题描述
经过使用其他语言的一些经验之后,我开始使用Java.对于所有这些人,我一直在使用Atom代码编辑器.因此,我使用Java进行了管理,但是最近我发现我需要使用外部库JFreeChart.
I'm starting with Java after some experience with other languages. For all of them I have been using Atom code editor. And so I have managed with Java but recently I have found out that I need to use an external library JFreeChart.
我正在使用JDK 8在cmd(Windows)上运行Java,并且没有使用任何IDE.
I am using JDK 8 to run Java on cmd (Windows) and I'm not using any IDE.
我到目前为止已经尝试过:
I tried so far:
javac -cp "lib/*" ./Test.java
在 lib 文件夹中具有 jfreechart-1.0.19.jar 和 jcommon-1.0.23.jar .
将Maven与pom.xml一起使用:
Using Maven with pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.codehaus.mojo</groupId>
<artifactId>my-project</artifactId>
<version>1.0</version>
<!-- https://mvnrepository.com/artifact/org.jfree/jfreechart -->
<dependencies>
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jcommon</artifactId>
<version>1.0.24</version>
</dependency>
</dependencies>
</project>
使用CLASSPATH系统变量:
Using CLASSPATH system variable:
D:\Study\Java\code\com\lib\jfreechart-1.0.19.jar
D:\Study\Java\code\com\lib\jcommon-1.0.23.jar
将jar文件放入 D:\ Program Files \ Java \ jdk \ jre \ lib \ ext
到目前为止,我总是得到:
and so far I always get:
error: package org.jfree.chart does not exist
我现在有点发疯了,所以我要寻求帮助.我该怎么做才能正确添加该库?
I'm kinda going nuts now so I'm asking for help. What can I do to add this library correctly ?
这是我的代码(也许我导入不正确):
Here is my code (maybe I'm importing incorrectly):
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.ChartFactory;
import org.jfree.data.general.DefaultPieDataset;
import java.io.File;
public class Test {
public static void main(String[] args) {
DefaultPieDataset pieDataset = new DefaultPieDataset();
}
}
完整的错误消息(在所有情况下都是相同的):
Full error message (in all cases it was the same):
D:\Study\Java\code\jfc> javac .\Test.java
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF8
.\Test.java:2: error: package org.jfree.chart does not exist
import org.jfree.chart.JFreeChart;
^
.\Test.java:3: error: package org.jfree.chart does not exist
import org.jfree.chart.ChartUtilities;
^
.\Test.java:4: error: package org.jfree.chart does not exist
import org.jfree.chart.ChartFactory;
^
.\Test.java:5: error: package org.jfree.data.general does not exist
import org.jfree.data.general.DefaultPieDataset;
^
.\Test.java:10: error: cannot find symbol
DefaultPieDataset pieDataset = new DefaultPieDataset();
^
symbol: class DefaultPieDataset
location: class Test
.\Test.java:10: error: cannot find symbol
DefaultPieDataset pieDataset = new DefaultPieDataset();
^
symbol: class DefaultPieDataset
location: class Test
6 errors
推荐答案
由于使用的是pom.xml
,因此您已经破坏"了项目,因此需要使用Maven进行构建.
Since you are using a pom.xml
you've "mavenized" your project and hence you need to use Maven to build it.
所以有几件事:
- 确保已正确下载,安装和设置Maven(它必须是路径的路径)...(如果尚未这样做的话)请检出 http://repo1. maven.org/maven2 ),并指定您本地的Maven存储库(它将库下载到的地方).
- 确保已根据标准项目结构对代码进行了结构化.
- Be sure you have downloaded, installed and setup Maven correctly (it needs to be path of your path) ... if you've not already done so check out Welcome to Apache Maven
- Make sure your
settings.xml
is configured correctly to point to a Maven repository (i.e. http://repo1.maven.org/maven2) as well as specify your local Maven repository (where it'll download the lib's to). - Ensure you have structured your code according to the standard project structure.
这意味着您的Test
类将需要放置在包org.codehaus.mojo
中.因此,根据标准项目结构,Test
将出现在目录结构D:\Study\Java\code\jfc\src\main\java\org\codehaus\mojo\Test.java
This would mean that your Test
class would need to placed in the package org.codehaus.mojo
. So according to the standard project structure, Test
would appear in the directory structure D:\Study\Java\code\jfc\src\main\java\org\codehaus\mojo\Test.java
- 您
pom.xml
应该坐在D:\Study\Java\code\jfc\
- 在与
./pom.xml
相同的目录中,运行mvn package
命令. - 如果一切设置正确,Maven将连接到您已配置的远程存储库,将这些软件包下载到本地存储库并构建一个JAR文件,然后您可以运行该文件.
- You
pom.xml
should sit inD:\Study\Java\code\jfc\
- In the same directory as your
./pom.xml
run themvn package
command. - If everything is setup correctly, Maven will connect to the remote repository you've configured, download those packages to your local repository and build a JAR file, which you can then run.
已通读五分钟之内的Maven
更新:包装具有依赖项的jar
.
UPDATE: Packaging a jar
with dependencies.
为了打包具有相关性的jar
,您需要将maven-assembly-plugin
添加到您的pom.xml
中.将其添加到pom.xml
并运行mvn package
后,它会生成(根据您的示例)名为my-project-1.0-jar-with-dependencies.jar
的jar
(您可以将descriptorRef
更改为更好的名称,也可以排除它) .此jar
应该包含项目需要的所有运行时依赖项.
In order to package a jar
with dependencies, you will need to add the maven-assembly-plugin
to your pom.xml
. Once you've added this to your pom.xml
and run mvn package
it should generate (according to your example) a jar
called my-project-1.0-jar-with-dependencies.jar
(you can change the descriptorRef
for a better name, or exclude it). This jar
should contain all the run-time dependencies your project needs.
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>org.codehaus.mojo.Test</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
已通读 Maven程序插件:用法
这篇关于如何将JFreeChart库添加到JDK?错误:软件包org.jfree.chart不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!