Jump to content




Recent Status Updates

  • Photo
      18 Aug
    KodeKool

    When faced with a wall of errors and no hope to fix them, remember the following "Programs always do what you tell them to, and seldom what you want them to, but eventually you'll run out of things that can go wrong and it'll just work. and that's the secret to good programming."

    Show comments (2)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

Package error (Not exist/Wrong name)

java

  • Please log in to reply
2 replies to this topic

#1 Cander

Cander

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 55 posts

Posted 01 March 2010 - 02:53 PM

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

package mainn;
import mainn.sub.test2;
public class test1
{
	public static void main(String[] arguments)
	{
	}
}

test2.java in folder C:\mainn\sub

package mainn.sub;
public class test2
{
}

test2.java compiles, but when compiling test1.java I receive the following error:
test1.java:2: package mainn.sub does not exist
import mainn.sub.test2;
                ^
1 error

If I remove the import statement, it compiles. But when running test1.java I receive this error:
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

What am I doing wrong?

Thanks in advance for help!

//Cander
  • 0

#2 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1,263 posts

Posted 01 March 2010 - 03:13 PM

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:
javac mainn\test1.java
That should work. To run it, you'll also need to run it outside the package folder, like so:
java mainn.test1
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.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#3 Cander

Cander

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 55 posts

Posted 02 March 2010 - 05:19 AM

Thanks, that helped! I didn't really understand how classpaths worked and how to compile/run the classes properly.

Edited by Cander, 02 March 2010 - 12:02 PM.

  • 0





Also tagged with one or more of these keywords: java