Import custom libraries in Java
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.
deep19
Updated on May 04, 2020Comments
-
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.