Rebecca Nelson
Rebecca Nelson

Reputation: 1296

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.

Upvotes: 20

Views: 74823

Answers (6)

Kirk
Kirk

Reputation: 5077

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.

Upvotes: 2

phill
phill

Reputation: 9

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

Upvotes: -1

Narayan Iyer
Narayan Iyer

Reputation: 1

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

Upvotes: -2

Andrew
Andrew

Reputation: 2568

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

Upvotes: 3

Sachin
Sachin

Reputation: 18747

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.

Upvotes: 29

Michael Borgwardt
Michael Borgwardt

Reputation: 346250

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.

Upvotes: 7

Related Questions