DEV Community

loading...

Can I import local class files in Java, like you can Python?

baenencalin profile image Calin Baenen ・1 min read

Can I do:

import localfolder.localclassfile;
Enter fullscreen mode Exit fullscreen mode

in Java?

Discussion (13)

pic
Editor guide
Collapse
siy profile image
Sergiy Yevtushenko

There is no notion of "current path" in regard to package structure in Java. Nor there are any parent-child relationship. In other words, every single package is independent and should be specified with the complete package name starting from the root. So, if you have package foo.bar and class within this package need to import some class from subpackage called localfolder (as in your example), you need to specify full import path: foo.bar.localfolder.LocalClassFile. You also can import nested classed from classes, for example foo.bar.localfolder.LocalClassFile.NestedClass.

Collapse
baenencalin profile image
Calin Baenen Author

Thank you! This is exactly what I'm looking for, it really helps with a "special prohect".

Here's a teasing hint: Interpreted language.

Again, thanks for the help.
Cheers.

Collapse
clo profile image
Christopher Lowenthal

It's been a while, so I may be completely off.
But files in the same folder are in the same namespace.
If you need something outside of the normal folder structure, it's possible to load a compiled bytecode file.
It's also possible to load text from a file, or anywhere, and compile it in real time.

I'm recalling from work on a game engine 12ish years ago. So it may even be easier now.

Collapse
baenencalin profile image
Calin Baenen Author

It's not in the same folder. customclass is in a folder below the package.

Like this:

package:
    example.java // I want to import files from subpackage here.
    subpackage:
        // Files....
Enter fullscreen mode Exit fullscreen mode
Collapse
clo profile image
Christopher Lowenthal

I'm also recalling that it's possible to create your own loader that determines the rules of where to find classes.
IIRC you could make it so your code above works, but not sure it's worth the effort.

Alrighty now my head hurts from digging that deep into past projects.

Collapse
thefern profile image
Fernando B 🚀 • Edited

If the class is in the same package you shouldn't have to do anything. If is in another package then you need to specify the package not the folder.

import anotherpackage.someClass;
Enter fullscreen mode Exit fullscreen mode
Collapse
baenencalin profile image
Calin Baenen Author • Edited

Will it be able to distinguish if the package is one directory down?
Like this:

Other code:
    // Files...
package 1: // I want to import package 2 here.
    somefile.java
    package 2:
        classes.class

Enter fullscreen mode Exit fullscreen mode
Collapse
thefern profile image
Fernando B 🚀

There isn't such thing as nested packages like you've shown. Packages are all independent of each other. Here's an explanation of hierarchy stackoverflow.com/questions/265092...

Thread Thread
baenencalin profile image
Calin Baenen Author

But, can a package be in a directory below the current package?
If not, where would the packages have to be, and how would I enforce this on to the computer of the user that uses my program?

Thread Thread
thefern profile image
Fernando B 🚀

Sorry is been a while since I last program in Java memory is a bit blurry. According to SO you can't have nested packages, but I just tried and I was able to create a package inside a package. So what you're trying to do is doable.

studytonight.com/java/subpackage-a...

Collapse
zoedreams profile image
☮️✝️☪️🕉☸️✡️☯️ • Edited

no, i would suggest reading this book deitel.com/java-how-to-program-11-...

you need to declare your package and make sure your including stuff from that.. stuff in java runs within a virtual machine and requires their own unique paths refered to classpaths which you specify when starting the jvm .. java cli

Collapse
baenencalin profile image
Calin Baenen Author

Question, if I have a package called runtdeale.code in the folder RuntDeale/resources/code, could I do import runtdeale.code.libs.org.json.*; to import the JSON library, which is a package below my original package?

Filesystem:

RuntDeale:
    resources:
        textures: ...
        ...
        code:
            Main.java
            manifest.txt
            ...
            libs:
                org:
                    json:
                        /* org.json library files */
Enter fullscreen mode Exit fullscreen mode
Collapse
shadowtime2000 profile image
shadowtime2000

From my knowledge (which isn't as in depth), I believe the organization of your file structure in Java has little to nothing to do with how you import them. If you want to be able to import another file you have to declare what package it is in and then import it from there. I don't use Java that much, so there is probably something that I am missing.