How to compile Java program with .jar library

65,853

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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.

Share:
65,853

Related videos on Youtube

Rebecca Nelson
Author by

Rebecca Nelson

NoSQL DBA, Infrastructure Engineer, and full-stack developer for with a focus on Graph databases, in particular Neo4J, and creation of tools to automate common workflow tasks. I work as a Sr. Engineer for Target Corporation, and I serve as the Technical Lead for the Engineering team of Moe Serifu Circle, a group dedicated to the research and development of anime-themed personal assistants.

Updated on January 02, 2022

Comments

  • Rebecca Nelson
    Rebecca Nelson 11 months

    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.

    • Andrew
      Andrew over 11 years
      you're importing acm.program... which you say is a package... not a class. To import a class from acm.program package you have to do 'import acm.program.CLASS', to import a single class OR you have import all the classes in the package with 'import acm.program.* ;'
  • Rebecca Nelson
    Rebecca Nelson over 11 years
    I don't want to run anything; I can't get my source code past the compiler because it extends a class from the jar file.
  • james_bond
    james_bond over 11 years
    He is trying to compile, this command is for running the compiled source
  • Rebecca Nelson
    Rebecca Nelson over 11 years
    The structure appears to be correct (I unzipped it to check), and I am running this on a Mac.
  • Rebecca Nelson
    Rebecca Nelson over 11 years
    This also did not work. Usually, I would use Eclipse, but it's just not feasible right now. I only want to compile one thing on this machine!
  • Rebecca Nelson
    Rebecca Nelson over 11 years
    Ah! the import statement was acm.program;! It should have been acm.program.*;.
  • Matthew G
    Matthew G about 9 years
    @Steve-o On Unix you wouldn't use semi-colons because the path separator is a colon.
  • phill almost 2 years
    thats supposed to be "jars/*"