• 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

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic