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?
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.
Joined: Dec 16, 2002
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!
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.