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 5 of 5

Thread: Package confusion

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Package confusion

    We have been supplied with a bunch of example code files for an assignment. Each file has
    package FabricCuts
    at the top. I can't get any of the files to run if I have the package stuff at the top, but if I remove the line from each file they run fine.

    What I've tried:
    1. Creating a new directory FabricCuts and moving all files inside.

    Here is the error I see with a bit of context.

    > ls
    Garment.java          GarmentFile.java      GarmentFileTest.java  Piece.java            PieceTest1.java       TextFile.java         TextFileTest.java
    > javac PieceTest1.java Piece.java 
    > java PieceTest1
    Exception in thread "main" java.lang.NoClassDefFoundError: PieceTest1 (wrong name: FabricCuts/PieceTest1)
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
            at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
            at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
            at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
            at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
            at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    >

    As I said everything works fine if I remove the package line from the top of each file, but I'd rather know how it is supposed to work.

    Thanks.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Package confusion

    If you have not yet, see Creating and Using Packages (The Java™ Tutorials > Learning the Java Language > Packages)
    In elementary terms, packages are basically directories. How are you trying to compile/run the classes? Command line? IDE?

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Package confusion

    Thanks copeg, I did stumble across that link, that's what tipped me off about creating a folder.

    I'm using the command line to compile and run classes. The commands I used (javac, java) can be seen in my original post.

    If it's relevent I'm using the diablo-jdk package on FreeBSD 8-STABLE

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Package confusion

    Sorry, missed the command-line text in your original post. Try compiling and running from the root of the package. If you have a folder named 'FabricCuts', and all .java files of the FabricCuts package are in that folder, go one directory up and type 'javac FabricCuts/myclass.java Same goes for running it.

  5. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Package confusion

    Ah ha. Thank you very much.

Similar Threads

  1. Method & Class confusion
    By jog98 in forum Object Oriented Programming
    Replies: 8
    Last Post: December 8th, 2010, 11:28 AM
  2. help 2d array confusion
    By Macgrubber in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 29th, 2010, 04:30 PM
  3. ArrayList confusion
    By Stormin in forum Collections and Generics
    Replies: 7
    Last Post: August 13th, 2010, 04:58 PM
  4. Default Access (package access) confusion
    By gauravrajbehl in forum Java Theory & Questions
    Replies: 1
    Last Post: November 18th, 2009, 04:11 AM
  5. Confusion in creating class with OOPS concept
    By grbsmj in forum Object Oriented Programming
    Replies: 3
    Last Post: May 6th, 2009, 03:14 AM