Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Don't understand putting classes in packages

 
Charles Knell
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What directories are in your CLASSPATH?
 
Charles Knell
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Charles Knell
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Charles Knell
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ding ding ding ding ding!

Yep, that's all exactly right.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic