Running Java program from command line

Discussion in 'Mac Programming' started by naples98, Jan 19, 2011.

  1. naples98 macrumors member

    naples98

    Joined:
    Sep 9, 2008
    Location:
    Houston
    #1
    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.
     
  2. lee1210, Jan 19, 2011
    Last edited: Jan 19, 2011

    lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Did you put your class in a package? If so, do you have the directory structure setup to match this package name?

    -Lee
     
  3. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #3
    Try not going into the helloworld directory, but staying in helloworld's parent directory. And then try this:

    Code:
    java helloworld.Main
    
     
  4. naples98 thread starter macrumors member

    naples98

    Joined:
    Sep 9, 2008
    Location:
    Houston
    #4
    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.
     
  5. naples98 thread starter macrumors member

    naples98

    Joined:
    Sep 9, 2008
    Location:
    Houston
    #5
    That worked. I guess I was trying to execute it from the wrong directory.

    Thanks!
     
  6. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #6
    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.
     

Share This Page