File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes how java looks for classes to load Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how java looks for classes to load" Watch "how java looks for classes to load" New topic

how java looks for classes to load

John M. Gabriele
Ranch Hand

Joined: Feb 18, 2001
Posts: 232
hoooo eeee! i'm gonna just be full of questions this week.
good ones too!
ok. when i go to run a simple one-class, no package-statement
little java program (from my bash command line), java doesn't
seem to be behaving the way i'd expect.
my current working directory is /home/me
i've got my little program in /home/me/myJavaPrograms/Factorial
and the file is, of course, Factorial.class
i'd expect i could run the thing right from where i am by
simply typing
java Factorial 8
the reason i would think that is b/c flanagan (nutshell book)
says, "By default, the interpreter uses the current working
directory as the root and looks for all classes in and beneath
this directory."
can anyone tell me why it doesn't work? further, it doesn't
work if i type anything like
java /home/me/myJavaPrograms/Factorial/Factorial 8
java /home/me/myJavaPrograms/Factorial/ Factorial 8
java /myJavaPrograms/Factorial/Factorial 8
java /myJavaPrograms/Factorial/ Factorial 8
but works fine, of course, if i cd over to the Factorial
directory and go java Factorial 8
John M. Gabriele
Ranch Hand

Joined: Feb 18, 2001
Posts: 232
hmm... just thought i'd try this:
java myJavaPrograms.Factorial.Factorial 7
but it doesn't work either...?
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
The behaviour you are seeing is correct, and as explained in Flanagan. If a class has no package, it will only be found if in a directory actually on the classpath (or at the top level of a jar file in the classpath). In the case where you say this works, your "current directory" is automatically added to the classpath and the class file is found by looking there.
If your current directory is anything else when you want to run the class, you will need to add the directory where the class resides to your classpath in order to run it.
Has this helped at all?

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
John M. Gabriele
Ranch Hand

Joined: Feb 18, 2001
Posts: 232
thanks for the reply frank,
my CLASSPATH includes . (my current directory).
i gathered, from the flanagan comment that
java does "deep searches" into CLASSPATH directories.
"By default, the interpreter uses the current working
directory as the root and looks for all classes in and beneath
this directory."

judging from your post (and my experiments) it seems that
the interpreter does not search subdirectories of directories
named as part of the CLASSPATH.
i now see that java myJavaPrograms.Factorial.Factorial 7
shouldn't have worked anyway b/c i didn't put a line like
package myJavaPrograms.Factorial;
at the beginning of (yes, i know it shouldn't
have caps.)
the reason this seems important to me is that i'm just
about to learn how to put my classes into packages and
thought i should understand this behaviour first.
John M. Gabriele
Ranch Hand

Joined: Feb 18, 2001
Posts: 232
if anyone's interested (and i know you are ) i finally
got the lo-down.
i made a few directories in my home/me directory. i put in a
com/jmg/num and in that directory i put the
file---but i added a line to the top of it:
package com.jmg.num;
I further changed my CLASSPATH to include home/me.
Now i can run my little Factorial program from any
directory i want.
see, (again, for any greenhorns who are interested)
when you put a directory in your class path, you're telling
the JRE where to look for classes it needs to load at runtime.
you're talking to the JRE.
when you put the package line in the .java file, you're talking
to the compiler. you're telling the compiler how to fully
qualify the class names of the classes you're about to declare.
when you're done compiling, the package line in the source file
has done its job, and javac has turned classnames like Factorial
into their buf fully-qualified names, like
when you go to execute your program from the command line,
you can execute the whole java command from anywhere 'cause:
a. you've included /usr/local/jdk1.3/bin (or some path like that) in your PATH, so the OS knows where to find java.
b. you've specifed the CLASSPATH so java knows where to start
looking for classes.
c. you've given the fully-qualified class name to be
[/etc]$ java com.jmg.num.Factorial 8

Joined: Oct 18, 2000
Posts: 10
Hi ,
I have been also trying hard to be able to execute my code from anywhere but I am unable to do so :
My files are lying in c:\myclasses
If I try to compile from the root directory C . It gives me an error that file not found .
I tried putting
package myclasses ;
as the first line of the java file so that compiler can know it is but still it is giving me an error .
Can u please help me where am i going wrong in my understanding !
Shall be thankful to you .
John M. Gabriele
Ranch Hand

Joined: Feb 18, 2001
Posts: 232
you want to compile right from the directory where the
.java file is. go to c:\myclasses to compile.
if you're only making small programs with a couple of classes,
you don't necessarily need the package statements in your source
files. since c:\myclasses is in your CLASSPATH, the JRE will be
able to find the .class files there.
btw, please see the Ranch's naming policy and change your
username accordingly.
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
Classpath is a burr in the bandana.
The classpath designates several root directories. Java starts "lookin" in these directories to resolve class references found in your code. For example, if you reference java.util.ArrayList then Java looks in its classpath for a directory named "java" and within that a directory named "util" and within that a file named ArrayList.class. (A jar file is really just a zip file -- those files were originally in a directory structure. When a jar file is in the classpath then Java treats that file as if it were the unzipped directory structure.)
If you're using packages, then the root of the directory structure designated by the package name must be in the class path. Furthermore, if you use packages then you must always refer to those class files via their package name (or implicitly using the package name in code using an import). For example, to run a Java application named MyApplication in package myapps you run java, passing "myapps.MyApplication" -- Java must then find the myapps subdirectory looking at the starting points you designate in your classpath.
John M. Gabriele
Ranch Hand

Joined: Feb 18, 2001
Posts: 232
right max, thanks.
i forgot about the fact that the compiler uses the CLASSPATH
when it needs to find classes that you import in your .java
files. i guess it needs to find these classes to make sure
they're all compiled up and ready to go.
i don't see any other reason it would need to look at these
class files tho... do you?
John Bateman
Ranch Hand

Joined: Mar 09, 2000
Posts: 320
Add "." and "./" to you classpath and your problem should go away.

SOURCE CODE should be SURROUNDED by "code" tags.
I agree. Here's the link:
subject: how java looks for classes to load
It's not a secret anymore!