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.
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
with jfreechart-1.0.19.jar and jcommon-1.0.23.jar in lib folder.
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>
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
Putting jar files in 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
Since you are using a pom.xml
you've "mavenized" your project and hence you need to use Maven to build it.
So there are a couple of things:
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).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
should sit in D:\Study\Java\code\jfc\
./pom.xml
run the mvn package
command.Have a read through Maven in Five Minutes
UPDATE: Packaging a jar
with dependencies.
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>
Have read through Maven Assembly Plugin : Usage