Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

naples98

macrumors member
Original poster
Sep 9, 2008
95
3
Houston
I usually use NetBeans to program in Java but wanted to try to compile and run from the command line. I wrote a simple Hello World program (Main.java) and compiled it from the command line (javac Main.java) with no problem but when I try to run it from the local directory using

java Main

I get the following error.

Code:
Exception in thread "main" java.lang.NoClassDefFoundError: Main (wrong name: helloworld/Main)
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
	at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:248)

Any ideas how I can fix this? I've looked around quite a bit but most suggestions haven't worked for me. I'm working on 10.6.6.
 
Did you put your class in a package? If so, do you have the directory structure setup to match this package name?

-Lee
 
Last edited:
Try not going into the helloworld directory, but staying in helloworld's parent directory. And then try this:

Code:
java helloworld.Main
 
Did you but your class in a package? If so, do you have the directory structure setup to match this package name?

-Lee

I just wrote the program in NetBeans with a package name of helloworld. I went to the directory (~/NetBeansProjects/HelloWorld/src/helloworld) where Main.java was located and compiled there. Running it from that local directory did not work.

Just to try, I removed the package name and relocated Main.java to ~ which compiled and ran fine.
 
Try not going into the helloworld directory, but staying in helloworld's parent directory. And then try this:

Code:
java helloworld.Main

That worked. I guess I was trying to execute it from the wrong directory.

Thanks!
 
Directories don't really matter in Java in the conventional sense. What matters is the directory that has the main method is somewhere in the class path.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.