Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By jim829

Thread: javac can't Find class in same package

  1. #1
    Peter B is offline Member
    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Default javac can't Find class in same package

    File src/Util.java
    Java Code:
    package com.foo;
    public class Util {
      public static int plus1(int i) {return i+1;}
    }
    File src/Minnow.java:
    Java Code:
    package com.foo;
    import com.foo.plus1;
    public class Minnow {
      public static void main(String[] args) {Util.plus1(2);}
    }
    Compiled with:
    javac -d bin -s src src/Util.java
    javac -d bin -s src src/Minnow.java

    Compiler Error Report:
    src\Minnow.java:2: error: cannot find symbol
    import com.foo.plus1;
    ^
    symbol: class plus1
    location: package com.foo

    Compiled from base
    source: base/src
    class: base/bin

    What did I do wrong?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: javac can't Find class in same package

    It took me a few minutes since I rely on an IDE to do this.

    First, change the import to
    import com.foo.Util;

    Then do the following:

    javac -d bin -s src -cp bin src/Util.java
    javac -d bin -s src -cp bin src/Minnow.java

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Peter B is offline Member
    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Default Re: javac can't Find class in same package

    Thanks Jim,
    It was driving me nutz.

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: javac can't Find class in same package

    Shouldn't the Util and Minnow class placed inside the src/com/foo directory?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: javac can't Find class in same package

    Yep.
    And the import is entirely unecessary as they are both in the same package anyway.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: javac can't Find class in same package

    A classic example of how we learn from our mistakes and not our successes!

    Regards,
    Jim
    gimbal2 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Javac Compiler Error[import package]
    By pateldpk in forum New To Java
    Replies: 27
    Last Post: 04-14-2012, 07:22 AM
  2. Replies: 22
    Last Post: 02-10-2012, 01:41 PM
  3. javac does not import package from jar in 1.6
    By tonydr in forum Advanced Java
    Replies: 0
    Last Post: 05-26-2010, 11:34 PM
  4. Replies: 1
    Last Post: 03-04-2010, 05:07 PM
  5. Replies: 3
    Last Post: 05-01-2008, 09:30 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
  •