Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error: About Package a class

 
Richard Jackson
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,all
During the course of studying the assignment,I did many short exercises.
To my surprise,I can't package a common class and then import it!
For instance,after build a directory called "package1",my simple test program(file is Package1.java) like this,

According the code:
1.Compiling passed.
2.Running program occursjava.lang.NoClassDefFoundError info.
In another test program,I found same error during the course of "package" and "import" .
I don't dare to say anything about the original problem.
But that's TRUE!
Could you please help me?
Regards,Richard
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Richard,
2.Running program occursjava.lang.NoClassDefFoundError info.

You should start your program from the parent directory of your package :
If your package1 is c:\tests\package1 and contains your Package1 class, you start Package1 from c:\tests.
Hope this helped.
Best,
Phil.
 
Richard Jackson
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,Philippe
Thanks for your reply.
You should start your program from the parent directory of your package :
If your package1 is c:\tests\package1 and contains your Package1 class, you start Package1 from c:\tests.

I did exactly that like you said again and again,and repeat all sorts of ways,"package"..."import",but...
All code shows as follow,

Another class is,

Please comment on if I was wrong with something.
Regards,
Richard
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Richard,
The issues you're facing with packages are a bit unclear to me. Do you have compile issues or just run time ones ? I'd like to post a simple but complete example of how sources are packaged. Unfortunately I am at my parents home (with no java at hand), but late tonight (europe time) when I am back home, I'll post such an example here.
In the meantime, the only comments I may do reading your code above are these :
  • Package names should be lowercased by convention.
  • Public classes file names should be exactly the same as the corresponding class names.


  • I guess that the issues you have are caused by a wrong directory structure. A package must reflect a directory structure. Example : If you develop your assignment in c:\scjd, a class named Data and packaged in suncertify.db should be saved in the file c:\scjd\suncertify\db\Data.java.
    If those explanations are enough to solve your problems, please tell me so I avoid writing the example announced above.
    Best,
    Phil.
     
    Richard Jackson
    Ranch Hand
    Posts: 128
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,Philippe
    Nice to see you again with discussion.
    As you said,
    A package must reflect a directory structure

    My sample package looks as follows,

    And their own code displays as above.
    In their seperate current directory,when I try to run it with " java PackageOne(or PackageTwo)" command,the program occurs exception "NoClassDefFoundError".
    So I estimate:
    1.The situation is not relational with uppercase.
    2.The key of problem is that the java run-time environment can't find the required class.
    Forgive me to take so initial problem.
    Could you have ways to solve it?
    Thanks in advance.
    Regards,
    Richard
     
    Philippe Maquet
    Bartender
    Posts: 1872
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Richard,
    After successful compilation, you may run your examples above this way :

    Output is :


    Output is :

    1.The situation is not relational with uppercase.

    Package and class names are case-sensitive : if you change the case in the examples above, you get the error you described :

    Now SUN's code conventions require that package names are in lowercase and class names start with an uppercase. It's just a convention but you should respect it.
    Best,
    Phil.
     
    Richard Jackson
    Ranch Hand
    Posts: 128
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,Philippe

    As you just said,I write the package name with uppercase and do the same operation like that.
    But it always fail.
    Please ensure two points:
    1.There is a space between the char '.' and 'R',in the "c:\>java -cp . Richard.PackageOne" command?
    2.The program was running at the Windows operating system/JDK 1.4.1 environment?
    Regards,
    Richard
    [ November 12, 2003: Message edited by: Jim Yingst ]
     
    Philippe Maquet
    Bartender
    Posts: 1872
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Richard,
    Please ensure two points:
    1.There is a space between the char '.' and 'R',in the "c:\>java -cp . Richard.PackageOne" command?

    Yes.
    2.The program was running at the Windows operating system/JDK 1.4.1 environment?

    1.4.2_22, but it shouldn't make any difference.

    Phil.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic