• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Package Problem?Pls Help

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Java Community
If you find my directory structure confusing from the paras below please refer to Dig. below:-
-----------------------------------------------------------------Part-I
-----------------------------------------------------------------I am using Windows 98 & facing problem with Packages or better to say at the time of compilation.,
I have three programs (1) Line.java (2) Point.java and the main() one (3)TryPackage.java stored as follow
C:\MYWORK\Packages\Geometry> contains Line.java & Point.java
C:\MYWORK\Packages\TryPackage> contains TryPackage.java
now when i compile TryPack like this:-
C:\MYWORK\Packages\TryPackage>javac -classpath "C:\MYWORK\Packages" TryPackage.java
it compiles fine but when i try to run the program like this
C:\MYWORK\Packages\TryPackage>java -classpath "C:\MYWORK\Packages" TryPackage
the following error ocurs
Exception in thread "main" java.lang.NoClassDefFoundError: TryPackage
Now how i over come the problem is that cut both my *.java & *.class file from the TryPackage folder delete the folder and paste both the files in the packages folder along with Geometry folder....compile them & run the application again it runs fine.
-----------------------------------------------------------------Part-II
-----------------------------------------------------------------Why do we need to compile our program when workin with package using the classpath stuff, i mean when i tried to compile TryPackage.java like
C:\MYWORK\Packages>javac TryPackage.java
and run it like
C:\MYWORK\Packages>java TryPackage
it works fine then also....so whats the purpose
Please if possible, refer me some easy to understand tutorial or just HELP ME OUT!
Regards
Azam Bukhari
[email protected]
-----------------------------------------------------------------Diagramatic View
-----------------------------------------------------------------
finally my directory structure looks like this @ the time of error free execution:-

[This message has been edited by Cindy Glass (edited December 03, 2001).]
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Azam,
Packages relate to the directory structure. When you are compiling, the JVM will search for the corresponding directory starting from the current directory.
Assuming that:

  • Line.java and Point.java both contain the package statement: <code>package Geometry;</code> and have <code>public</code> access
  • TryPackage.java contains the package statement <code>package TryPackage;</code> and has public access and contains an import statement <code>import Geometry.*;</code>

  • Make sure you are in the directory <code>c:\mywork\packages</code> and compile using the following command:
    <code>javac -d . Geometry\*.java</code>
    Followed by
    <code>javac -d . TryPackage\*.java</code>
    The '-d' tells the compiler to place the class files in the the package directory. The '.' tells it to begin its directory search from the current directory.

    Hope that helps.
    ------------------
    Jane Griscti
    Sun Certified Programmer for the Java� 2 Platform
    Co-author Mike Meyers' Java 2 Certification Passport
    [This message has been edited by Jane Griscti (edited December 02, 2001).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic