Playing with Java and for some reason I’m getting this error:
Exception in thread "main" java.lang.NoClassDefFoundError: AddingMachine (wrong name: sum/AddingMachine) 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: AddingMachine. Program will exit.
I decided to Google it, like I do when ever I get an error of any kind. Then I remembered bing, and thought this is an ideal moment to try it out.
Bing being new isn’t an excuse that they can use to defend it; Microsoft has has years experimenting with search and should know just about as much as Google now, but they’re still making school boy errors. For instance, this forum post is listed second in the results.
Bing should know it’s a forum, it’s pretty obvious. Then by that it could deduce that the person starting the thread is asking a question, likely my question. But there are no replies, so it’s clear that no one has answered it. Giving me that link is completely pointless. Google learnt that lesson years ago.
Back to the Java problem, I tried doing java -classpath . AddingMachine rather than just java AddingMachine and I get the same error…
Fix’d: I just copy-pasted the source from the java notes page I’m looking at. And the problem lied in it have a package sum; line. Just remove it.
Edit for protip: This is an error you’re running into because of packages, used to make java source files easier to manage. You should definitely check out this tutorial on packages if you’re having problems; Java Package Tutorial.