wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Don't understand putting classes in packages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Don Watch "Don New topic
Author

Don't understand putting classes in packages

Charles Knell
Greenhorn

Joined: Mar 19, 2006
Posts: 25
I have two classes (inititally) that I'd like to put into the same package. Call them ClassA and ClassB. The source files for both are in the same directory:

dev
..|---src
........|---com
..............|---kilo
.....................|---xtract

On the first line of each .java file I have:
"package com.kilo.xtract;"

ClassA compiles without complaint.

In ClassB, I try to create an instance of ClassA, but the compiler tells me it can't find it ("cannot find symbol").

It's plain that there's something fundamental about making packages that I don't understand.

Can someone throw some light on this for me? Thanks.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
What directories are in your CLASSPATH?
Charles Knell
Greenhorn

Joined: Mar 19, 2006
Posts: 25
Ah Ha! (light bulb begins to glow dimly over my head). None of these.

What do I need, the path to the directory with the compiled version of ClassA, or something more involved?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I'm betting you're running the compiler with "xtract" as your current directory, like

javac B.java

Don't do that. Your current directory should be "src" and you should use

javac com/kilo/xtract/B.java

Yes, that's a lot of typing: in real life, you'd use something like Ant to build your software.

Anyway, based on the package name, the compiler is looking for con/kilo/xtract/A.class, and not finding it. That's why you want to have "src" as your current directory. There are other ways to deal with this, but this is the most straightforward one.


[Jess in Action][AskingGoodQuestions]
Charles Knell
Greenhorn

Joined: Mar 19, 2006
Posts: 25
Yes, you were correct. I was trying to compile from "xtract".

I have a parallel set of directories for the .class files:

dev
..|---src
..|.....|---com
..|...........|---kilo
..|..................|---xtract
..|---bin
........|---com
..............|---kilo
.....................|---xtract


I tried compiling with "src" as the current directory. I used the -cp command-line switch ("-cp %CLASSPATH%;../bin/com/kilo/xtract"), but I still got an error from the compiler telling me that it couldn't find the symbol.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Compiling in the src directory, you need something like

javac -d ../bin -cp .:../bin com/kilo/xtract/B.java

The "-d" says where to put the class files, while the -cp says where to find previously-created ones.
Charles Knell
Greenhorn

Joined: Mar 19, 2006
Posts: 25
Compiling in the src directory, you need something like

javac -d ../bin -cp .:../bin com/kilo/xtract/B.java

The "-d" says where to put the class files, while the -cp says where to find previously-created ones.


I was under the impression that CLASSPATH told the JVM where to find "classes", that is to say, compiled java programs, files with a ".class" extension.

Your advice appears to be telling me to put into the CLASSPATH the path to the source code of the class instead. What am I missing here?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
If you have

../bin/com/kilo/xtract

in your classpath, the compiler will search for

com.kilo.xtract.B

as the file

;../bin/com/kilo/xtract/com/kilo/extract/B.class

That is, it takes the directory from the classpath, adds a folder for every package fragment for the class, and then searches the class in that path.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Charles Knell
Greenhorn

Joined: Mar 19, 2006
Posts: 25
Thanks! That clears things up. To recapitulate:

1) Compile from the directory that contains the sub-directory corresponding to the first element of the package name. For example, if the directory structure is like this:

/dev/java/src/com/kilo/xtract
and
/dev/java/bin/com/kilo/xtract

and the package statement reads "package com.kilo.xtract;"

you should compile from /dev/java/src.

2) If the .class files are in the directories under /dev/java/bin and you are compiling from /dev/java/src, then the classpath should contain the string "../bin". That is to say, "go up one directory level and then down the directory tree to "bin".

3) The compiler will read the package statement from the .java file and starting at /dev/java/bin, it will navigate down through the directory tree to find the necessary .class file(s) by appending to "../bin" each directory name listed in the package statement in order from left to right so that the effective classpath in this example will be "../bin/com/kilo/xtract".
[ April 26, 2006: Message edited by: Charles Knell ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Ding ding ding ding ding!

Yep, that's all exactly right.
 
GeeCON Prague 2014
 
subject: Don't understand putting classes in packages