Can I do:
import localfolder.localclassfile;
in Java?
Can I do:
import localfolder.localclassfile;
in Java?
For further actions, you may consider blocking this person and/or reporting abuse
Anwar -
Arshi Saxena -
Cathtine Zhamotsina -
Anwar -
Top comments (12)
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 calledlocalfolder
(as in your example), you need to specify full import path:foo.bar.localfolder.LocalClassFile
. You also can import nested classed from classes, for examplefoo.bar.localfolder.LocalClassFile.NestedClass
.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.
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.
It's not in the same folder.
customclass
is in a folder below the package.Like this:
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.
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.
Will it be able to distinguish if the package is one directory down?
Like this:
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...
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?
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...
Question, if I have a package called
runtdeale.code
in the folderRuntDeale/resources/code
, could I doimport runtdeale.code.libs.org.json.*;
to import the JSON library, which is a package below my original package?Filesystem:
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.