simple java problems

Discussion in 'Mac Programming' started by caustic, Jul 8, 2008.

  1. caustic macrumors newbie

    Jul 8, 2008
    hi there
    i am using osx 10.4.11. i am using the head first java book to do my first java application. it seems i am having a very common problem with an error of:

    Exception in thread "main" java.lang.NoClassDefFoundError: Beersong (wrong name: chap01/BeerSong)

    here is my terminal interaction:


    rich-Computer:~/Desktop/head first java/chap01 rich$ javac
    richs-Computer:~/Desktop/head first java/chap01 rich$ ls
    BeerSong.class HelloWorld.class PhraseOMatic.class
    rich-Computer:~/Desktop/head first java/chap01 rich$ java Beersong
    Exception in thread "main" java.lang.NoClassDefFoundError: Beersong (wrong name: chap01/BeerSong)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(
    at Method)
    at java.lang.ClassLoader.loadClass(
    at sun.misc.Launcher$AppClassLoader.loadClass(
    at java.lang.ClassLoader.loadClass(
    at java.lang.ClassLoader.loadClassInternal(
    rich-Computer:~/Desktop/head first java/chap01 rich$

    as you can tell, my java program ( compiles fine but i get this error. i am using the default settings within OSX for java development so nothing has changed. i have also ensured that the compiled class file of BeerSong.class is local to the file. should this not work? what am i doing wrong or how do i get this working?
  2. Enuratique macrumors 6502

    Apr 28, 2008
    Looks like you don't have your classpath setup correctly. Here's a fix straight from the horse's mouth (go with the Unix version)

    Having looked more closely at your terminal interaction, is your Beersong class in a namespace called chap01? If so, you need to navigate to ~/Desktop/head first java/ and type

    java chap01/Beersong

    I know this sounds counter intuitive, but that's how the java interpreter expects the namespace/folder structure to be.

Share This Page