Home > Net >  How can the Java compiler know were are the packages I create?
How can the Java compiler know were are the packages I create?

Time:01-02

I was wondering if there is any way that allow us to import packages in a class using a relative path.

To set an analogy, in html you can use a relative path to set an hyperlink.

For example, given this Files Structure:

    Folder: "A"
          |
          |____________ Folder: "B"
          |            |
          |            |
          |            |
          |            |_________  File html: "Link.html"
          | 
          |   
          |
          |________ File html: "Index.html"

So, in the file "Link.html" I can set this Hyperlink:

<!DOCTYPE html>
<html>
      <head>
           <title>Link</title>
      </head>
      <body>
           <main>
             <h1>You are in the file "Link"</h1>

               <!--This would be the hyperlink-->
               <a href="../Index.html"> Go to Index </a>

          </main>
      </body>
</html>

My question is: Could I create a package for example in folder "B" and import the class from that folder "B" in a java file which would be in folder "A"? I mean without using "classpath" (I don't say without using "sourcepath" too, because I'm not sure about how it works).

    Folder: "A"
          |
          |____________ Folder: "B"
          |            |
          |            |
          |            |
          |            |_________  File Java: "Example.java"
          | 
          |   
          |
          |________ File Java: "Main.Java"

Moreover, could I set an absolute path in a java file directly to import a package without having to tell the compiler, manually, where the packages are through classpath? (Like in html?)

I mean something like this example but when importing a package in java:

 <a href="C:\Users\Pc2\Desktop\A\Index.html"> Go to Index </a>

Thanks in advance ;)

CodePudding user response:

I was wondering if there is any way that allow us to import packages in a class using a relative path.

No. Java doesn't support that. Package names are fully qualified when you define them and fully qualified when you use them.


My question is: Could I create a package for example in folder "B" and import the class from that folder "B" in a java file which would be in folder "A"?

For the OpenJDK Java tool chain, and anything else that uses the same class resolution mechanisms1, the folder name structure must map to the package structure. So assuming that the folder above A is the root of your (source or binary) class folder tree, then your Main.java file needs a package declaration

package A;

and your Example.java file needs a package declaration:

package A.B;

(Note that case is significant, and package names A and B are a Java style violation.)

Class Example can import Main and vice versa, but they must use the fully qualified package names to do that; i.e.

import A.Main;
import A.B.Example;

Q: Can you trick the compiler / runtime by having a class / package naming scheme that doesn't correspond to the file / folder structure?

A: No. Either the Java compiler or runtime will fail to find classes or it will detect that there is a mismatch a class's fully qualified and the place where it was found. In both cases you will get an error.

Q: Can you import by file pathname?

A: No. The Java language doesn't permit this. Just ... no.


I mean without using "classpath" (I don't say without using "sourcepath" too, because I'm not sure about how it works).

That doesn't make sense. You cannot "not use" a classpath. If there is no classpath, the Java tools cannot find your code. There is always a classpath ... even if it is just the current directory; i.e. ".".


1 - As far as I am aware, this covers all current practical Java implementations. However, it is technically possible (and permitted by the Java Language Specification) for a Java implementation to use some other mechanism for organizing and locating source code or binary code "entities".

CodePudding user response:

For your first question: Java packages are used to do just that, but the CLASSPATH is still in use. The default class loader will look under the CLASSPATH for classes and expect them to be located based on their package names. (There are other class loaders that have different behaviors, but mostly you will use the default one.)

For your second question: No you cannot. As explained above, the default class loader will look under the CLASSPATH for any class you import, and will try to locate based on the package name of classes. (Again, assuming you use the default class loader.)

Java packages are the standard way of structuring your code, and for most cases this structure is also replicated on the file system (for both source and class files).

  •  Tags:  
  • java
  • Related