Cratylus
Cratylus

Reputation: 54074

Problem in packages in importing in Eclipse

I am trying to import some existing projects into Eclipse. The structures for their packages is:

Project/
        /src
          /java
             /a
              /b
                /c

Once imported in the package explorer I see:

Project
  src/java
       --a
         --b
            --c
               - AClass.java

This is ok, since the classes e.g. AClass.java are defined in package: a.b.c But in one project the structure (once imported) becomes:

Project
  src
     --java
        --a
          --b
            --c
              - AClass.java

And that causes the error that AClass.java is defined to be in package a.b.c but it is actually under java.a.b.c
Why is this happening? Why in this specific project java is not ignored as part of package?
Thanks

Upvotes: 6

Views: 29770

Answers (4)

Use this sentence import java.io.*; at the top of java file. Otherwise, you have to create package folder.

Import statements:

In Java if a fully qualified name, which includes the package and the class name, is given then the compiler can easily locate the source code or classes. Import statement is a way of giving the proper location for the compiler to find that particular class.

For example, the following line would ask compiler to load all the classes available in directory java_installation/java/io :

import java.io.*;

Upvotes: 0

fmucar
fmucar

Reputation: 14548

Remove the existing source folders first. -right click -> menu -> build path -> remove from build path

then

Right click on the source folder. build path -> use as source folder.

Seems like your settings are pointing to the parent of the source folder so src is recognized as package by eclipse.

Wrong package name when using automatically added imports in Eclipse

Upvotes: 5

aditya
aditya

Reputation: 495

call the package on the top of your import statements,

like if your class is in java/main/org/goal/Main.java

then the path is package java.main.org.goal;

else do Ctrl +1 and it suggest some quick help

import the necessary package from that

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499860

How are you creating the Eclipse projects? It sounds like you just need to put "java" as a root on on the source path here, instead of "src". You can do this by editing the build path after the import process, of course.

Upvotes: 8

Related Questions