2

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
Mortimer
  • 300
  • 5
  • 11
  • Your `Test` compiles with v1.019, but fails with 1.5, which no long requires jcommon; see [migration](https://github.com/jfree/jfreechart#migration-from-jfreechart-10x) for details. – trashgod Sep 25 '19 at 01:52
  • See also [*CLASSPATH vs java.ext.dirs*](https://stackoverflow.com/q/5039862/230513). – trashgod Sep 25 '19 at 02:32

1 Answers1

2

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:

  1. 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
  2. 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).
  3. Ensure you have structured your code according to the standard project structure.

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

  1. You pom.xml should sit in D:\Study\Java\code\jfc\
  2. In the same directory as your ./pom.xml run the mvn package command.
  3. 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.

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

Ambro-r
  • 919
  • 1
  • 4
  • 14
  • Thanks man, it helped a lot. Now I got another error! That's a lot of progress. Do you by any chance now why I got `Exception in thread "main" java.lang.NoClassDefFoundError: org/jfree/data/general/DefaultPieDataset at app.jfc.App.main(App.java:15)` or should I make another question for this? I googled and no help once more. No ones uses JDK :) – Mortimer Sep 24 '19 at 19:35
  • `java.lang.NoClassDefFoundError` indicates, that something that may have been found at **compiletime** is not being found at **runtime**. How are you running your `jar`? – Ambro-r Sep 25 '19 at 06:04
  • firstly after adding dependencies into pom.xml I run **mvn package**. When build has been created I run **java -cp target/test-jfc-1.0-SNAPSHOT.jar app.jfc.App**. [Image of my structure](https://i.imgur.com/D799D0B.png) – Mortimer Sep 25 '19 at 07:12
  • Ok, so firstly ... an good IDE makes life easy and makes one forget things :). As I mentioned previously your lib's are being found at **complietime** not **runtime**. So in order for them to be found at **runtime** the best option is to have them packaged into your JAR. I'll put in an edit above to help with this step. – Ambro-r Sep 25 '19 at 15:12
  • O k. I don't know what I'm doing wrong. And I will leave it at that. I don't have enaough time for this. Thank you very much for all this but I can't waist more time. – Mortimer Sep 25 '19 at 15:37
  • Check out my update on _Packaging a jar with dependencies_ – Ambro-r Sep 25 '19 at 15:40
  • Hey, as I though nothing works (the jar isn't generated) but I have read the guide and I have a question: doesn't this plugin declaration miss ****? And I'm attaching my pom.xml [Pom on MediaFire](http://www.mediafire.com/file/jj7rs0cmgoauxog/pom.xml/file) maybe you will find something but if that's a pain in the back I'm ready to give up. – Mortimer Sep 25 '19 at 15:59
  • Just had brief look and only thing I can see off-hand is that you've wrapped your `` tags with ``. The `` tag is _intended to configure project builds that inherit from this one_. So for now, for your build just remove everything and keep your `` tag simple as per the example posted (only change `app.jfc.App`) – Ambro-r Sep 25 '19 at 16:31
  • @Ambro-r: Can you migrate the `` guidance to your answer? – trashgod Sep 26 '19 at 09:36
  • @trashgod, um sure ... migrate it to where though? A bit unsure what your asking. – Ambro-r Sep 26 '19 at 10:14
  • @Ambro-r: Sorry, I was confused; I defer to you on whether the answer needs any additional information form the comments. – trashgod Sep 27 '19 at 01:21