File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes How used to package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How used to package " Watch "How used to package " New topic

How used to package

zhang feng

Joined: Dec 16, 2002
Posts: 19
i creat two class-files (eg, 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?
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

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:
likewise, if you had:

Your directory structure would have to be:

Then you have to run "java mypackage.MyClass1" to run the program.
Hope that this helps,

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
zhang feng

Joined: Dec 16, 2002
Posts: 19
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!
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
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.

I agree. Here's the link:
subject: How used to package
It's not a secret anymore!