Hello,
I have trying to sorting my classfiles in my project into folders and using the Java Package function, but I'm experiencing problems nevertheless after trying to following any tutorials about this I could find.
I made a very simple test just to trying to get this work, here is what I got:
test1.java in folder C:\mainn
test2.java in folder C:\mainn\subCode:package mainn; import mainn.sub.test2; public class test1 { public static void main(String[] arguments) { } }
test2.java compiles, but when compiling test1.java I receive the following error:Code:package mainn.sub; public class test2 { }
If I remove the import statement, it compiles. But when running test1.java I receive this error:Code:test1.java:2: package mainn.sub does not exist import mainn.sub.test2; ^ 1 error
What am I doing wrong?Code:java.lang.NoClassDefFoundError: test1 (wrong name: mainn/test1) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$000(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClassInternal(Unknown Source) Could not find the main class: test1. Program will exit. Exception in thread "main" >Exit code: 1
Thanks in advance for help!
//Cander
This is because the mainn folder is NOT on the classpath. In order to build it using javac, you'll need to go outside the mainn folder and say this:
That should work. To run it, you'll also need to run it outside the package folder, like so:Code:javac mainn\test1.java
This is because Java will automatically append the current directory to the classpath, which will then properly resolve the position mainn.sub.test2 as the class.Code:java mainn.test1
Wow I changed my sig!
Thanks, that helped! I didn't really understand how classpaths worked and how to compile/run the classes properly.
Last edited by Cander; 03-02-2010 at 12:02 PM.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks