command line 'java' only works in the present working directory

I have compiled a test class Simple.class which does nothing but prints its name

public class Simple {

public static void main(String[] args) {

System.out.println("I'm a simple class!");

}

}

following is form the Bash Terminal, on Mac OS X:

G5:~/Java user$ ls

Simple.class

G5:~/Java user$ java Simple

I'm a simple class!

G5:~/Java user$ cd ..

G5:~ user$ java Java/Simple

Exception in thread "main" java.lang.NoClassDefFoundError: /Users/user/Java/Simple

-

it seems like the command line 'java' only works for me when the present working directory is the same as of the class I am trying to invoke?

why?

[712 byte] By [krzna] at [2007-10-3 4:18:16]
# 1
[url= http://java.sun.com/j2se/1.5.0/docs/tooldocs/findingclasses.html]How classes are found[/url][url= http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/classpath.html]Setting the class path[/url]
TimTheEnchantora at 2007-7-14 22:19:55 > top of Java-index,Java Essentials,New To Java...
# 2

When you provide the name of the class, it has to include its full package name (your's doesn't have a package) and the parent directory of the root of the package has to be on the claspath. In your case: java -cp Java Simple

It works in the first case because if there's no classpath specified and no env var set, then the classpath is assumed to be the current directory.

jverda at 2007-7-14 22:19:55 > top of Java-index,Java Essentials,New To Java...
# 3
Because "java Java/Simple" means you want to start class named Java.Simple. Do you have one? No. So you should change your code by adding package name or specify classpath within your command line.java -classpath Java Simple
Michael.Nazarov@sun.coma at 2007-7-14 22:19:55 > top of Java-index,Java Essentials,New To Java...
# 4
thanks a lot to all of you for clearing that up!
krzna at 2007-7-14 22:19:55 > top of Java-index,Java Essentials,New To Java...