thanks, that helps.
its purpose seems clear, but its utility less so.  is there a best
practice or design pattern regarding -cp you can recommend?


ooo.stephen
===========

On Aug 26, 1:46 pm, Mihai DINCA <[email protected]> wrote:
> Hi ooo.stephen
>
> This is a good question, because it worth to understand the "classpath"
> philosophy in the beginning, once for all.
>
> In brief, Java CLASSPATH is a list of directories representing the place
> where Java looks for the (fully qualified with the package name) .class
> files, in about the same way Unix/Linux looks for its programs in $PATH
> and Windows in %PATH%. The difference is that Java classes use a package.
>
> A .java file use a "package" statement in the beginning of the code to
> specify its package. After compiling, the ".class" file "knows" that it
> can be called only by specifying the package. For e.g. "java -cp C:\foo
> bar.test" will execute the "main" static method of the file
> "C:\foo\bar\test.class".
>
> But "java -cp C:\     foo.bar.test" will not work and "java -cp
> C:\foo\bar test" neither because "test.class" "knows" that it should be
> called as "bar.test" only (as the file "test.java" contains "package
> bar" at the beginning).
>
> Now, where should "java bar.test" look for a subdirectory "bar"
> containing a file "test.class"? The answer is in the CLASSPATH.
>
> The CLASSPATH is a list of directories separated by ":" under Unix/Linux
> (and OS-X I think) and by ";" under Windows. The Java loader will scan
> each of the directories in the CLASSPATH, in sequence, to find the
> requested files.
>
> You can set the CLASSPATH by defining an environment variable containing
> the list of directories. Such as:
> -- Unix/Linux/OS-X
>     export CLASSPATH=/usr/tst:/var/lib:/var/tst
>     java bar.test
>
> -- Windows
>     set CLASSPATH=C:\libs;D:\Users\myself\Documents\tests
>     java bar.test
>
> You can override the value of the currently defined CLASSPATH variable
> on the command line. Such as:
> -- Unix/Linux/OS-X
>     export CLASSPATH=/usr/tst:/var/lib:/var/tst
>     java -cp /home/myself/tst:/home/myself/lib bar.test
>
> -- Windows
>     set CLASSPATH=C:\libs;D:\Users\myself\Documents\tests
>     java -cp
> D:\Users\myself\Documents\proj;D:\Users\myself\Documents\proj\lib bar.test
>
> (No need to say that "cp" stands for CLASSPATH).
>
> If no CLASSPATH variable is used and no -cp command line option, then
> Java use some default classpath (that can be modified too, but that is
> another matter) that is usually ".".
>
> The classpath (the variable or the -cp option and so on) may also
> contain a .jar file too (or more), allowing to look for the requested
> package/file.class in the .jar file(s) too.
>
> Hope it helps
> Mihai
>
> ooo.stephen a crit :
>
> > my grasp of this is a little shaky.  please correct me if i'm wrong,
> > but my understanding (and maybe my articulation will help others), is
> > the following:
>
> > 1- -cp statement essentially counter acts/accommodates the package
> > path statement.  this is because the package statement is a directive
> > to look for .class files in the path stated in the package, starting
> > in the current dir the file is being executed from.  in order to
> > compile from the command line, you need to set the -cp path to a dir
> > that would allow the package path to be true, i.e.:
> > E:\Programming\Java Passion\basic\10_classpath\homework\foodpackage
> > \fruitpackage>
> > java -cp ../../ foodpackage.fruitpackage.FoodMain
>
> > 2- -cp is not necessary when running the 'java' command with a true
> > package path. that is, if you run 'java' from two dirs above, the
> > following is true;
> > E:\Programming\Java Passion\basic\10_classpath\homework>
> > java foodpackage.fruitpackage.FoodMain
>
> > same as:
> > E:\Programming\Java Passion\basic\10_classpath\homework>
> > java -cp . foodpackage.fruitpackage.FoodMain
>
> > i'm having a hard time conceptualizing a real world example or need
> > for dealing with the class path.  because the package statement must
> > match the dir structure, changing the class path seems undesirable,
> > and would require recompiling the .class file.
>
> > thanks kindly,
> > ooo.stephen
>
>

-- 
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to