Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Howiieque

macrumors regular
Original poster
Feb 1, 2009
120
0
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
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
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.
 

iShater

macrumors 604
Aug 13, 2002
7,026
470
Chicagoland
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.
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
If you do:

java /Users/howiieque/Desktop/Foo

it will work.

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'
 

Howiieque

macrumors regular
Original poster
Feb 1, 2009
120
0
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?
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
By default, where does the CLASSPATH environment specifies?

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'.
 

Howiieque

macrumors regular
Original poster
Feb 1, 2009
120
0
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?
 

wrldwzrd89

macrumors G5
Jun 6, 2003
12,110
77
Solon, OH
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?
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.
 

Howiieque

macrumors regular
Original poster
Feb 1, 2009
120
0
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.:)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.