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

Thread: package for Beginners

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default package for Beginners

    Respected PROGRAMMERS

    When a classfile that belongs to a package, then

    package PackageName;


    is included in the source code of that file. So when jvm is invoked by writing

    java PackageName.classfilename

    it gets executed.

    Is it that "package PackageName" guarantees the jvm that this classfile belongs to this very package? Because if we omit the "package PackageName" statement, then jvm still finds out the class file but gives

    Exception in thread "main" java.lang.NoClassDefFoundError: Classfilename
    wrongname PackageName/ClassfileName
    It means jvm finds out the file but there is some reason for which it considers that this classfile has a wrong name.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: package for Beginners

    As far as i know, you have to fix the environment variables, in order to let jvm know about the exact location of your file. Coz your class file is not set as a main project file. And in your environment variable CLASS_PATH, you have given the path to the directory. So, it is able to find the package but not class.
    Also, you can use editors. Editors allow you to set any project as main project.
    I don't know, if this is the answer, you asking for.

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: package for Beginners

    Once the JVM finds the class you are looking for, it checks the first line for that package statement. If the package statement isn't there, it assumes the class, the command or SOMETHING was named incorrectly. The package statement is absolutely required in a source file to to make sure both the compiler and the JVM at runtime understand what classes should be where.

    This has nothing to do with Class Path if I understand your question correctly...

  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 for Beginners

    The package statement is absolutely required in a source file to to make sure both the compiler and the JVM at runtime understand what classes should be where
    Package statements are not required. Packages help organize code as well as define the class path, but are not required.

Similar Threads

  1. where is the java tutorial for beginners
    By javaque in forum Java Theory & Questions
    Replies: 2
    Last Post: September 14th, 2011, 03:30 AM
  2. Simple package problem, package does not exist error
    By Farmer in forum Object Oriented Programming
    Replies: 3
    Last Post: August 23rd, 2011, 11:18 AM
  3. YAY for beginners
    By derbya in forum Member Introductions
    Replies: 2
    Last Post: August 16th, 2011, 04:54 AM
  4. Java Binary Tree (Beginners)?
    By IAmHere in forum Algorithms & Recursion
    Replies: 6
    Last Post: June 19th, 2011, 10:28 AM
  5. [SOLVED] Books and sources for Java beginners
    By chronoz13 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 15th, 2009, 08:36 AM

Tags for this Thread