• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with Packages

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having troubles working through a package example. I have a directory named Flora where my source code is located (along with the compiled class files). The following code compiles/executes fine:

The last line prints out and everything is great. However, when I uncomment the first line in the first file and the first two lines in the second file, the programs will still compile but I get a java.lang.NoClassDefFoundError:Forest2 (wrong name Flora/Forest2). Can some shed some light on this for me?
 
Chris Allen
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found the answer to my question above. If I remove the package statement in the second file, everything works fine. I think it is because I am trying to put a class (Forest2) into the same package that I am trying to import from (Flora). Is this a correct assumption?
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>>Can some shed some light on this for me?

I�ll try.

C:\Java\Exam\flora\Tree.java
C:\Java\Exam\flora\Forest2.java


Version 1. My current directory is C:\Java\Exam

C:\Java\Exam>javac flora\Forest2.java

C:\Java\Exam>java flora.Forest2
The tree's age is 0

Version 2. My current directory is C:\Java\Exam\flora

C:\Java\Exam\flora>javac �classpath .. Forest2.java

C:\Java\Exam\flora>java -classpath .. flora.Forest2
The tree's age is 0

Two things I have to keeping reminding myself.
1. When executing the program, use the fully qualified name of the class flora.Forest2

2. When compiling and executing the program, set the classpath to the directory that contains the package directory flora.

----

>>I think it is because I am trying to put a class (Forest2) into the same package that I am trying to import from (Flora).

It is okay to import a public class from any package.

>>Is this a correct assumption?

No.

----

Figuring out how to use packages with javac and java can be frustrating. Don�t give up. Keep practicing. You need this skill to understand protected.

1. When compiling and executing the program, you can be in ANY directory anywhere. Set the classpath to contain the package directory.

2. When compiling, qualify the filename with directories if necessary, depending on your current directory. >javac dir\filename.java

3. When executing, always qualify the class name with the package name(s).
>java pkg.classname
[ June 20, 2004: Message edited by: Marlene Miller ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic