• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

how java looks for classes to load

 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
thanks!
 
John M. Gabriele
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm... just thought i'd try this:
java myJavaPrograms.Factorial.Factorial 7
but it doesn't work either...?
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
John M. Gabriele
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Factorial.java (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
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Factorial.java
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
com.jmg.num.Factorial
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
interpreted:
[/etc]$ java com.jmg.num.Factorial 8
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
I have been also trying hard to be able to execute my code from anywhere but I am unable to do so :
PATH=%PATH%;C:\MSSQL7\BINN;C:\JDK1.3\BIN;C:\myclasses;
SET CLASSPATH = .;%CLASSPATH%;C:\JDK1.3\SRC.JAR;C:\myclasses
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 myclasses.filename.java 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
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Add "." and "./" to you classpath and your problem should go away.
 
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic