Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Cannot find class in same package

  1. #1
    Junior Member j831526's Avatar
    Join Date
    Jun 2014
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Cannot find class in same package

    package package_play;
    // file name is PP.java in package_play directory
     
    public class PP {
        public static void main(String[] argv) {
            Flubber f = Flubber("Hi fool");
            System.out.println(f.msg);
        }
    }
     
    // file name is Flubber.java in package_play directory
    package package_play;
     
    public class Flubber {
        String msg;
        Flubber(String x) {
            msg = x;
        }
    }
    I compile Flubber.java running javac from package_play directory - no problem
    I then compile PP.java running javac from package_play directory - error: cannot find Flubber
    I must be missing something basic. CLASSPATH is not set. My understanding is it will default to current directory.
    Thanx for any suggies - oh, please don't suggest I use your favorite IDE instead of the command line

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Cannot find class in same package

    compile PP.java running javac from package_play directory - error: cannot find Flubber
    When compiling and executing the class file package folder must be on the classpath. The classpath must point to the folder holding the package_play folder.

    Copy and paste here the contents of the command prompt window showing the current directory and the command being used.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member j831526's Avatar
    Join Date
    Jun 2014
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Cannot find class in same package

    The CLASSPATH is not set. The default is supposed to default to the current directory which is the directory containing both files.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Cannot find class in same package

    Use the javac command option: -cp to set the classpath for the compile step.
    The path to the class file should be <classpath><package>
    For example if the path to the class file is = C:\Work\package_play\Flubber.class
    then the class path must point to the C:\Work folder which contains the package_play folder.
    The classpath option could be: -cp .;..\.
    the ..\. refers to the next folder up from the current folder. You would use that if in the package_play folder.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member j831526's Avatar
    Join Date
    Jun 2014
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Cannot find class in same package

    Forgot to mention I'm on a Mac. Here's a recap:
    package package_play; # File name PP.java
     
    public class PP {
        public static void main(String[] argv) {
            Flubber f = new Flubber("Hi fool");
            System.out.println(f.msg);
        }
    }
     
    package package_play; # file name Flubber.java
     
    public class Flubber {
        String msg;
        Flubber(String x) {
            msg = x;
        }
    }

    position myself in the package_play directory
    javac -cp ../ Flubber.java => works
    javac -cp ../ PP.java => also works
    java PP => fails
    Error: Could not find or load main class PP
    Caused by: java.lang.NoClassDefFoundError: package_play/PP (wrong name: PP)
    java -cp ../ PP => fails - same error as above
    BTW: if I remove the package statements I can compile both files and run the program without any classpath.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Cannot find class in same package

    package_play/PP (wrong name: PP)
    The full class name includes the package name: package_play.PP

    java -cp ../ package_play.PP
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member j831526's Avatar
    Join Date
    Jun 2014
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Cannot find class in same package

    Bingo!!
    Added package_play to java command & it works.
    Norm, thanx for staying with this with your help
    ... Charlie

Similar Threads

  1. Replies: 149
    Last Post: February 19th, 2013, 10:04 PM
  2. Replies: 1
    Last Post: February 14th, 2013, 07:30 PM
  3. [SOLVED] Compiling Package, cannot find symbol
    By Actinistia in forum Java Theory & Questions
    Replies: 1
    Last Post: November 6th, 2012, 02:19 PM
  4. is it possible to extends java class with another class from different package?
    By fredsilvester93 in forum Java Theory & Questions
    Replies: 6
    Last Post: July 20th, 2012, 03:51 PM