Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
559 views
in Technique[技术] by (71.8m points)

compiler construction - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...