java.class can't run

Discussion in 'Mac Programming' started by Howiieque, Mar 21, 2009.

  1. macrumors regular

    Joined:
    Feb 1, 2009
    #1
    Code:
    public class Foo {
    	
    	public static void main (String[] args) {
    		System.out.println("Hello world!");
    	}
    }
    I compiled it and ran it in the terminal:

    mac:~ howiieque$ javac /Users/howiieque/Desktop/Foo.java
    mac:~ howiieque$ java Foo
    Exception in thread "main" java.lang.NoClassDefFoundError: Foo
    mac:~ howiieque$

    I wonder what's wrong??:(
    newbie for Java
     
  2. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #2
    It's because this Foo class is not in your classpath.

    So you could do one of the following:

    Code:
    java -cp /Users/howiieque/Desktop Foo
    
    This adds /Users/howiieque/Desktop to your classpath so that the classloader can find the class Foo. (Note there is a space between Desktop and Foo)

    OR you can do:

    Code:
    cd /Users/howiieque/Desktop/
    java Foo
    
    Because when you run 'java' from a shell, the current directory you are in is part of the classpath by default.

    Hope that helps.
     
  3. macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #3
    You need to be in the directory where that .class file is or it has to be in your CLASSPATH.

    If you do:

    java /Users/howiieque/Desktop/Foo

    it will work.
     
  4. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #4
    Well, that won't work, actually. Because that's just the same as 'java Users.howiieque.Desktop.Foo'

    The parameter there is a classname, not a filepath. So he needs to do 'java -cp /some/path SomeClass'
     
  5. thread starter macrumors regular

    Joined:
    Feb 1, 2009
    #5
    Such a crystal clear explanation! Eddietr, thank so much.
    Before posting this thread, I had tried what iShater said and can't work.

    By the way, I looked at the java manual.
    -cp classpath
    Specifies a list of directories, JAR archives, and ZIP archives
    to search for class files. Class path entries are separated by
    colons :)). Specifying -classpath or -cp overrides any setting
    of the CLASSPATH environment variable.

    If -classpath and -cp are not used and CLASSPATH is not set, the
    user class path consists of the current directory (.).
    By default, where does the CLASSPATH environment specifies?
     
  6. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #6
    Well, unless you define a CLASSPATH variable in your environment, generally speaking it will be null.

    So in that case the application classloader will use your current directory '.' as the classpath for your app.

    You can set CLASSPATH in your shell to point to some other list of directories, or jar files (or both) that your application needs. Or you can specify those in the -cp parameter to 'java'.
     
  7. thread starter macrumors regular

    Joined:
    Feb 1, 2009
    #7
    Again, thanks.:D
     
  8. thread starter macrumors regular

    Joined:
    Feb 1, 2009
    #8
    Again, a silly question.
    I put code into two separate java file, as follow:
    A.java
    Code:
    public class A {
    	int test=13;
    	public void setint(int i) {
    		test=i;
    	}
    	public int getint () {
    		return test;
    	}
    }
    
    B.java
    Code:
    public class B {
    	public static void main (String[] args) {
    		A a=new A();
    		System.out.print(a.getint());
    	}
    }
    In terminal. If I compile the B.java, the compiler can find A. What should I do?

    But I used the textedit, everything works well. Why?
     
  9. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #9
    Can you copy the exact commands you are using and the exact output?
     
  10. macrumors G4

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #10
    Are you compiling/running from the Terminal? If you are, are B.java and A.java in the same folder? If so, then...

    Code:
    cd /path/to/folder/where/classes/are/
    javac A.java
    javac B.java
    java B
    ...ought to work.
     
  11. thread starter macrumors regular

    Joined:
    Feb 1, 2009
    #11
    You're very nice. Thank you very much.
    I don't know why. But today I tried many times and everything worked properly.

    Last time, I made sure that both two classes were in the same directory. I forgot the what the exact message was but it just meant it couldn't find class A.

    By the way, the last post has a spelling mistake. I meant textmate instead of testedit.

    Again, thank you.:)
     

Share This Page