简体   繁体   中英

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.

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.

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 .

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

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

The no brainer issue is 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

Should follow both case compilation or running.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM