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.
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. 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 ...
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).
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:
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With