javajar

How to compile Java program with .jar library


I can't make javac recognize an external .jar file, whose classes I'm trying to extend. I have two files in the same directory: TestConsole.java and acm.jar. I'm compiling from the same directory using the following command:

javac -classpath .:acm.jar TestConsole.java

But it seems like javac is just ignoring acm.jar. It gives me the error:

TestConsole.java:1: package acm does not exist
import acm.program;
          ^

Of course, acm.program is a package in acm.jar. All of the classes in acm.jar are already compiled; I just want to use them in my classes, not compile them.

What am I doing wrong?

I am running this on a Mac, and the directory structure of acm.jar appears to be valid: It contains an acm/program directory, which has ConsoleProgram.class, the only class that TestConsole extends.

javac -classpath ".:acm.jar" TestConsole.java does not work, either.


Solution

  • Check list:

    1. your classes in acm.jar appear as:

      acm/program/CLASSX.class

      acm/program/CLASSY.class

      when decanted with jar tf acm.jar

    2. You're importing them like:

    import acm.program.CLASSX ;

    or

    import acm.program.* ;