I'm currently refreshing my Java knowledge and came across a question I couldn't answer yet:

How does the Java compiler know where the (possibly custom) packages are located on disk? I'd assume that using built-in libraries it looks up the java library folder - but how does it find the custom ones?

Here's the file's locations for the following example:

// part of the package

// not part of the package

Is the naming convention to be seen as relative path? If it's this, why do we need to write "import package com.usuallyNot.example;" (instead of "import package "usuallyNot.example;") even if the file we write it in resides in the "com" folder?

2 Answers 2


The answer is the classpath:

Classpath is a parameter—set either on the command-line, or through an environment variable—that tells the Java Virtual Machine or the Java compiler where to look for user-defined classes and packages.

There are instructions from Oracle how to set your classpath.

The classpath is searched in order of the folders that appear for a class matching the import. There are lots of details in how multiple classes of the same name can co-exist.

The classpath is set either by %CLASSPATH% (Windows DOS shell, $env:CLASSPATH in PowerShell) or $CLASSPATH in Unix or in the manifest of a Jar file.

You can see the future of classpath from the 2015 JavaOne keynot (starting @ 17 minutes).

  • So I assume my .java package declarations shouldn't work out if the directories used aren't in the PATH variable. I wonder if that's one of the tasks an IDE like eclipse performs when asking for a workspace.
    – UsuallyNot
    Oct 26, 2015 at 15:46
  • @UsuallyNot well it also does I believe auto imports jars in the local folder as well. It's not a pretty elegant story. Often with Maven we produce "super jars" that "pour in" all the classes of our dependencies in with our classes into one jar.
    – Sled
    Oct 26, 2015 at 17:20
  • Well, I do not know how to create .jar-archives without using an IDE yet. I also just created .jar files for just one project - not to bundle libraries, but I want to get to know this in near future.
    – UsuallyNot
    Oct 26, 2015 at 17:28

If you didn't import your types, the compiler would have to perform expensive hard disk scans every time it encounters a custom type. That is very expensive; it's better to force the programmer to declare the location explicitly, so the compiler only has to open one file directly. (An additional advantage is that you can have several types with the same unqualified name, but efficient compilation is the big one.)

  • My question is not if I should use packages at all, it's about how the java-compiler finds the right folders (and therefore files) if the example2.java file is located anywhere on the disk. I'd like to know if the package definition implicites the file location in the file system.
    – UsuallyNot
    Oct 26, 2015 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.