simple java problems

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

  1. caustic macrumors newbie

    Joined:
    Jul 8, 2008
    #1
    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 BeerSong.java
    richs-Computer:~/Desktop/head first java/chap01 rich$ ls
    BeerSong.class BeerSong2.java IfTest.java PhraseOMatic.java
    BeerSong.java HelloWorld.class PhraseOMatic.class helloworld.java
    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(ClassLoader.java:620)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    rich-Computer:~/Desktop/head first java/chap01 rich$

    ----------
    as you can tell, my java program (BeerSong.java) 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 BeerSong.java file. should this not work? what am i doing wrong or how do i get this working?
    thanks
     
  2. Enuratique macrumors 6502

    Joined:
    Apr 28, 2008
    #2
    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