So I have a package named com.company.example, and I want to import it in my .class file. Where do I put that com.company.example package? I don't understand "classpath" please do not use it in answers. I have tried import com.company.example.*; but no packages found
Thanks.
You don't import packages into class files. You import packages into Java source code so you can use the classes in them without using the full name of the class. The packages you import don't become part of the class file.
Sorry, but the answer to the question is to use the class path (
CLASSPATH
). The class path is a list of directories where.class
and.jar
files can be found. When you use a class from a package, the default classloader in the JVM searches the class path to find a.class
file (directly, or inside a.jar
) that provides the package you imported. (There are other classloaders, but the normal case is the one that searches through theCLASSPATH
for.class
files.)So for example: Let's say you want to use the class
com.company.example.Foo
in your code. You could do this:...but that's inconvenient, so you do this at the top of your source file:
...and then this in your code:
Either way, your
.class
file contains your code and does not contain theFoo
class you're using.At runtime, when the JVM sees that you want to use
com.company.example.Foo
, it will look through theCLASSPATH
for.class
files (or.jar
files containing.class
files) to find one that providescom.company.example.Foo
. When it does, it uses the class from that file.Because the classpath is fundamental to useing Java, I recommend reading up on it.