Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 791
Rebecca Nelson Avatar asked May 20 '11 00:05

Rebecca Nelson


People also ask

How to compile and run Java programs with external libraries?

How to Compile and Run Java Programs with External Libraries. If you use an IDE like Eclipse to develop your Java programs then using external libraries is pretty straight forward. You just need to add the JAR file to your project and then right click on it and add it to the build path, and the IDE will do the rest of the work for you.

How to compile and run Java program step by step?

How to Compile and Run Java Program. In this section, we learn how to compile and run java program step by step. Step 1: Write a program on the notepad and save it with .java (for example, DemoFile.java) extension. Step 2: Open Command Prompt. Step 3: Set the directory in which the .java file is saved. In our case, the .java file is saved in C ...

How do I create an executable JAR file in Java?

Create executable JAR file using jar command The first line specifies the class will be called when the JAR file gets executed. This class must have main () method. You see the StudentsInsert class is specified with its fully qualified name (including package name).

How to add dependency JAR files to class path in Java?

Try to add all dependency jar files to your class path through environment variable settings or use the below steps: Open command prompt. Change directory to the location of you java file that you would like compile. Set the classpath for your dependency jar files as shown below:


2 Answers

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.

like image 121
Bhushan Avatar answered Sep 17 '22 23:09

Bhushan


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.

like image 28
Michael Borgwardt Avatar answered Sep 21 '22 23:09

Michael Borgwardt