File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How used to package " Watch "How used to package " New topic
Author

How used to package

zhang feng
Greenhorn

Joined: Dec 16, 2002
Posts: 19
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?
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1815
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,


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

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.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How used to package
 
Similar Threads
Calling super constructor from subclass
Scope of variable declarartions in for loop
Mock exam queries
why don't protected inner class compile?
Top level class??