Results 1 to 4 of 4
  1. #1
    spamsickle is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default Eclipse "import not resolved" package problem?

    I downloaded the Java2D demo package from java sun com/products/java-media/2D/samples/suite/Java2Dsamples.zip, unzipped it, and dragged the resulting src folder to a src folder in a new Java Project I created in Eclipse.

    There are three "key" modules in the src folder proper:

    DemoSurface.java
    DemoPanel.java
    CustomControls.java

    These are referenced in virtually every module in several sub-folders under src, such as src\demos\Clipping\Areas.java

    Areas.java begins with a "package" statement - "package demos.Clipping;"

    A subsequent statement, "import DemoSurface;" is flagged as an error, "The import DemoSurface cannot be resolved."

    I suspect this is because DemoSurface.java is in my project as belonging to the "default" package (there is no package statement in DemoSurface.java), but I'm not sure what the quick solution to the problem is under Eclipse. I hope there is some simple change I can make to the project configuration, but nothing I've tried so far has resolved the problem.

    Can somebody help a newbie out?

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    if DemoSurface.java is in src\demos\ABC\

    then the import statement should be import demos.ABC; or import demos.ABC.DemoSurface;

  3. #3
    spamsickle is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default

    No, DemoSurface is just in src:

    src\DemoSurface.java

    The import statements which are failing are in subdirectories of src.

    src\demos\Clipping\Areas.java

    package demos.Clipping;
    ...
    import DemoSurface;

    I've tried the obvious (& I guess, obviously stupid)
    import ..\..\DemoSurface;

    Eclipse flags this as a syntax error immediately. Same with "import \DemoSurface;" or "import /DemoSurface;".

    I'm sure this compiled right out of the box back when Java2D was new, and I don't think things have changed that much since then, but being a newbie I don't know the easy way to fix what's wrong today. I suspect I can add a "path" somewhere, and all these problems will go away. Failing that, perhaps there's something in the "refactor" capabilities of Eclipse that will accomplish it pretty painlessly.

    One thing I haven't tried yet is putting DemoSurface in its own subdirectory of src, and changing all the dozens of import statements to include that subdirectory.

    Or maybe I could put all the \src classes in a jar file, and add that to the project configuration as a library?

    If I copy DemoSurface.java from \src to \src\demos\Clipping\, and add a package statement to it ("package demos.Clipping;"), the error in Areas.java goes away. That can't be the real solution, however, as there are several other subdirectories of \src\demos (Clipping, Arcs_Curves, Colors, Composite, etc), each of which has multiple modules with its corresponding "package" statements, and the "\src" classes all reference each other too... I'd have to copy over a dozen \src classes into over a dozen subdirectories, which would mean the slightest program change would become a nightmare.

    I'm sure this is something that's obvious to someone with experience, and I'm hoping one of those "someones" will post a solution. Thanks for your help.
    Last edited by spamsickle; 08-22-2009 at 02:10 PM.

  4. #4
    spamsickle is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default

    Wow, 65 views and no solution, guess I should have posted this in the "Advanced" section instead of the "Newbies" section. Or maybe nobody reads this section except all the other newbies.

    Anyway, I found a solution on my own. Newbie power! Now, I'm the guy with experience I was hoping for in my second post!

    In the "Project Explorer," I highlighted the root for my project, right clicked, and chose "New -> Package". In the "New Java Package" wizard, I named my new package "core" and clicked "Finish".

    With the new package created, I returned to the "Project Explorer," highlighted everything in the "default package" by clicking on the first one and shift-clicking on the last one, then dragged the whole group into my new "core" package.

    Eclipse responded by graciously changing all the references in every module from "import DemoSurface" to "import core.DemoSurface." What a pal!

    I still have a few issues with constants in switch/case statements. For example, in src\demos\Arc_Curves\Curves.java, the following code

    Java Code:
                        PathIterator f = shape.getPathIterator(null);
                        while ( !f.isDone() ) {
                            float[] pts = new float[6];
                            switch ( f.currentSegment(pts) ) {
                                case f.SEG_MOVETO:
                                case f.SEG_LINETO:
                                    g2.fill(new Rectangle2D.Float(pts[0], pts[1], 5, 5));
                                    break;
    gives me "case expressions must be constant expressions" for f.SEG_MOVETO and f.SEG_LINETO, even though in PathIterator.class they're defined as
    Java Code:
    public static final int SEG_MOVETO		= 0;
    public static final int SEG_LINETO		= 1;
    Looks like the problem here is that these values are constant at RUN time but not at COMPILE time, because "f" is an instance of the class, which doesn't exist at compile time. I'm not sure how to solve it yet, but if my brief experience here is any indicator, I'll bet I figure it out before one of you posts a solution.

    ETA: Well, that was hardly fair. Turns out, the solution is simply to change f.SEG_MOVETO to PathIterator.SEG_MOVETO.
    Now, the whole project compiles under Eclipse without errors. Thanks anyway, I'm off to explore.
    Last edited by spamsickle; 08-24-2009 at 12:09 PM.

Similar Threads

  1. Replies: 0
    Last Post: 04-20-2009, 06:26 PM
  2. Replies: 5
    Last Post: 01-27-2009, 03:49 PM
  3. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. Dependencies "cannot be resolved"
    By Jesdisciple in forum Eclipse
    Replies: 0
    Last Post: 05-03-2008, 06:54 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •