Reputation: 91
I am having trouble accessing a couple of classes in separate folders for a project I have in school. I am using Geany, and need to be able to have these classes talk to each other, so to speak.
There is one main folder in which all the separate packages are contained as their own folders, and within each folder there are all the classes specific to that package.
In my case, I need to be able to have some of the classes in a gameObjects package for use in a components package. I tried
import gameObjects.Room;
import gameObjects.Entity;
but I get this compile error:
Position.java:3: error: package gameObjects does not exist
Is there any way to get these classes to talk to eachother? Thanks in advance.
EDIT: The directory path looks like this:
C:\Users\matt\Desktop\New folder\Project
and in that Project folder are multiple folders where separate classes are contained. I need to be able to have two classes talk to each other from different folders within my Project folder.
I am compiling like so
C:\Users\matt\Desktop\New folder\Project\components>javac Position.java
EDIT #2: Both the Room and Entity classes are contained in a folder/package called gameObjects, and they, along with every other class in that folder contains
package gameObjects;
as its first line.
Upvotes: 2
Views: 2141
Reputation: 3609
Putting .java
files in separate folder in explorer doesn't mean that you specify any package for them. To have an ability to import classes, you need to make sure that you specify a package for them. You achieve it with putting:
package gameObjects;
line at the very beginning of the proper .java
file. After specifying that package, you can import classes contained in that package with:
import gameObjects.nameOfClass;
Note that if you don't specify a package
at the beginning of .java
file, it's then contained in default package (you have access to classes contained in default package only from other classes contained in default package too). You can't import classes that don't have a package nameOfYourPackage;
statement at the very beginning of .java
file, so you can't use them outside of default package.
Note that Java takes care of putting .java
files into specific folders on your hard drive on its own. So, when you specify that your sample.java
file is contained in yourpackage
, there is a folder named yourpackage
created in your project directory and sample.java
is put into that folder.
Upvotes: 0
Reputation: 46
I am having trouble accessing a couple of classes in separate folders for a project
Folder basically are packages. So class Room and Entity cannot be in different folders because as you wrote:
import gameObjects.Room;
import gameObjects.Entity;
These classes should be in "gameObjects" folder/package. So I guess the structure of your project is wrong. If you want to keep them in different folders then it will have different package.
Position.java:3: error: package gameObjects does not exist
Put both classes in gameObjects folder then add in the first line of each file "package gameObjects;"
Upvotes: 1