PDA

View Full Version : java.class can't run




Howiieque
Mar 21, 2009, 10:00 AM
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
Mar 21, 2009, 10:19 AM
It's because this Foo class is not in your classpath.

So you could do one of the following:


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:


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
Mar 21, 2009, 10:19 AM
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
Mar 21, 2009, 10:26 AM
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
Mar 21, 2009, 11:14 AM
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
Mar 21, 2009, 12:37 PM
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
Mar 21, 2009, 09:25 PM
Again, thanks.:D

Howiieque
Mar 22, 2009, 01:43 AM
Again, a silly question.
I put code into two separate java file, as follow:
A.java
public class A {
int test=13;
public void setint(int i) {
test=i;
}
public int getint () {
return test;
}
}
B.javapublic 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?

eddietr
Mar 22, 2009, 12:06 PM
Can you copy the exact commands you are using and the exact output?

wrldwzrd89
Mar 22, 2009, 12:23 PM
Again, a silly question.
I put code into two separate java file, as follow:
A.java
public class A {
int test=13;
public void setint(int i) {
test=i;
}
public int getint () {
return test;
}
}
B.javapublic 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...

cd /path/to/folder/where/classes/are/
javac A.java
javac B.java
java B
...ought to work.

Howiieque
Mar 23, 2009, 11:33 AM
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.:)