• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Don't understand putting classes in packages

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What directories are in your CLASSPATH?
 
Charles Knell
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ding ding ding ding ding!

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