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

How used to package

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i creat two class-files (eg myclass1.java,myclass2.java)in a same path,and i use myclass1's object in the myclass2
the two .class work right;
now, i add the package myclass; statement in the two the class-files ,then i complie corrcetly,it's throw the Exception : NoclassDefFoundError will i run.
i dont know what's happened?
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your directory structure must match your package structure. So (assuming that . is in your classpath), you had:

You would have to have your directory structure as:
./MyClass1.class
./MyClass2.class
likewise, if you had:

Your directory structure would have to be:
./mypackage/MyClass1.class
./mypackage/MyClass2.class

Then you have to run "java mypackage.MyClass1" to run the program.
Hope that this helps,
 
zhang feng
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you! my class work good for your method.
But i don't know that the class don't run at . path;
as you say ,i have two class at class/mypackage

i use "java mypackage.myclass2 run the myclass2.class at class/mypackaye
it run good;
but i repalce the "package mypackage;" to "package mypackage1;" ,the myclass1 compile good ,the myclass2 don't compile.
the compile-operator don't creat the directory itself?
thank you very much!
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simply put ...
You need to have your source files organized in a directory heirarchy. So, class file 'Test' in package 'com.javaranch' will be translated into a file '/com/javaranch/Test'.
After which, you will simple invoke your main function in Test by 'java com.javaranch.Test' from the '/' context. So, 'cd /' before you do it, or however you get there on whatever OS you're zipping along on.
You would probably want to compile the source files from the same folder also. The reason of course being that the compiler will find 'packaged' source files starting from the directory that you invoke the compiler from.
Yep. Um, that's it.
 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic