Eclipse packages and folders and such

I am building a project with eclipse but I am not familiar with their folder structure. I want to create a folder to hold a bunch of game objects of a certain type (trying to organize), but they are extending a class from another folder ... When I try to do this, I get errors saying that the class cannot be resolved to the type ... If i create the same game object in the superclass folder, it works fine ...

How do I extend classes from another folder?

How do I create a folder in the workspace view that doesn't affect links between folders?

+2


source to share


3 answers


I am assuming you are working in Java.

Java packages directly map to directories under the original directory replacement. with / (or \ on Windows). If you want to have something like this:

src --+-- dir1 -+-- Foo.java
      |         +---Bar.java
      +   dir2 ---- Baz.java

      



Then, to refer to the Baz class in Foo.java, you must use dir2.Baz

. As a shortcut, you can add

import dir2.Baz;

      

at the top of Foo.java and refer to the Baz class as soon as Baz

.

+3


source


Straying too little off the line here, it is also perfectly possible to split the solution into multiple source folders, but still have them in the same package. This will not make organizing your source code particularly "organized" or pleasant.

Then each source directory can contain a package named exactly the same (since the directory part of the package name is always relative), and when it is all compiled they will belong to the same package.

Here's a reference .



This will lead to something like this

workspace --+-- src1 -- mypackage + -- Foo.java
            |                     + -- Bar.java
            +-- src2 -- mypackage + -- Baz.java

      

where you can still access all the stuff in the files without explicitly importing them.

+1


source


I'm going to assume this is a Java project in Eclipse; if not, please provide additional information.

When using classes from different packages, you need to import them. This is done with one or more import statements at the top of the class file, for example:

import java.util.List;
import java.util.ArrayList;

      

This tells the compiler, "When I refer to List, I want to use the one in the java.util package."

In Eclipse, a convenient way to try and add these imports automatically is to right-click in the source editor and select Organize Imports under Source. Eclipse will look at all the code and libraries it knows about related projects and try to fix missing imports. If he cannot do something, he will either offer you or ignore it. If he finds several possibilities, he will usually ask you to choose the one you had in mind.

For a more detailed explanation of packages and source code splitting, see the Packages Lesson section in the Java Tutorials.

0


source







All Articles