17
votes

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.

6
you're importing acm.program... which you say is a package... not a class. To import a class from acm.program package you have to do 'import acm.program.CLASS', to import a single class OR you have import all the classes in the package with 'import acm.program.* ;' - Andrew

6 Answers

26
votes
javac -cp <jar you want to include>;<jar you want to include> <source.java> 

<jar you want to include> if in same directory, just name of jar will do, if not, specify full or relative paths

if more than one jars, separate with ,

replace ; with : on unix

If possible, use some IDE like Eclipse. I used to spend a lot of time on similar things, but in industry, you will hardly ever do it in this fashion.

6
votes

Are you running these commands on a Windows machine? On Windows, the elements of the classpath are separated by a semicolon, not a colon. So:

javac -classpath .;acm.jar TestConsole.java

Another possibility: the structure of acm.jar is wrong. It's not sufficient that the class files inside were compiled from files that declare package acm.program - the package structure must also be represented as a directory hierarchy, so acm.jar must contain a directory acm, and within that a subdirectory program that contains the actual class files for the classes used in TestConsole.

3
votes

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.* ;

2
votes

Whoever is trying to compile and still having the problem as I struggled for hours, I tried all the answers above and still was not able to run the program due to one minor issue.

The no-brainier issue was the semi colon after every package. I am not sure about Mac or Linux but for Windows Command Prompt this was the case

javac -cp mysql-connector-java-8.0.12.jar; Testing.java

java -cp mysql-connector-java-8.0.12.jar; Testing

You might wanna follow this both cases either in compilation or while running.

-1
votes

Many years behind but i struggled with this syntax, this worked for me to add all jar files plus compile with all classes in the program to the main class

My File Tree: Store classes .java files jars .jar files
images .PNG files

command line:

C:\Store>javac -cp "jars/" classes/.java classes/storeMain.java

-2
votes

I'm just adding for folks who are still looking for the answer to the same problem after successful compilation.

While compiling use the command as suggested above by @Michael Borgwardt:

javac -classpath .;acm.jar TestConsole.java

For executing also you need to specify the class path:

java -classpath .;acm.jar TestConsole