Import custom libraries in Java

32,063

The import statement imports classes from the jar file, not the jar file itself.

An import statement of the form:

  import stdlib.*;

will import all the classes in the package stdlib.

Oracle provides this tutorial on import.

EDIT: It looks like you're using the stdlib.jar for An introduction to programming in Java. The classes in this jar file have no packages. You don't need to import classes in the default package, since your class is also in the default package.

Share:
32,063
deep19
Author by

deep19

Updated on May 04, 2020

Comments

  • deep19
    deep19 about 4 years

    I need to import a Java class in my project. The class I need to import is in a JAR file with absolute path ~/Documents/mylib/stdlib.jar

    Now in my source file (Test.java) I am using

    import stdlib.*;

    and I am compiling with javac -classpath ~/Documents/mylib/stdlib.jar Test.java

    And it is showing the error that stdlib does not exist.

    Is my import statement correct or is there anything wrong with classpath? Shouldn't this import statement import all the classes present in the JAR file?

    I am not using any IDE and my OS is Linux.