Understanding Packages

S

shaun

Hi,

I am reading Ivor Horton's Beginning Java 2. I have the following
package setup:

C:\Java\Packages\Geometry

In this directory I have 2 classes:

Line.java
Point.java

These classes have compiled correctly. I have created a directory in
geometry called Shape2D and in this directory created a file called
Rectangle.java, here are the contents:

package Geometry.Shape2D;

public class Rectangle {
// Data members
private Point topLeft;
private Point bottomRight;

// Create a rectangle from two points
public Rectangle(final Point topLeft, final Point bottomRight) {
this.topLeft = new Point(topLeft);
this.bottomRight = new Point(bottomRight);
}
}

When I try to compile this program i get the foillowing error:

C:\Java\Packages\Geometry\Shape2D>javac -classpath
".;C:\Java\Packages\Geometry"
Rectangle.java
Rectangle.java:5: cannot find symbol
symbol : class Point
location: class Geometry.Shape2D.Rectangle
private Point topLeft;
^
Rectangle.java:6: cannot find symbol
symbol : class Point
location: class Geometry.Shape2D.Rectangle
private Point bottomRight;
^
Rectangle.java:9: cannot find symbol
symbol : class Point
location: class Geometry.Shape2D.Rectangle
public Rectangle(final Point topLeft, final Point bottomRight) {
^
Rectangle.java:9: cannot find symbol
symbol : class Point
location: class Geometry.Shape2D.Rectangle
public Rectangle(final Point topLeft, final Point bottomRight) {
^
Rectangle.java:10: cannot find symbol
symbol : class Point
location: class Geometry.Shape2D.Rectangle
this.topLeft = new Point(topLeft);
^
Rectangle.java:11: cannot find symbol
symbol : class Point
location: class Geometry.Shape2D.Rectangle
this.bottomRight = new Point(bottomRight);
^
6 errors

As you can probably tell I am a beginner so any tips here would be
greatly appreciated.

Shaun
 
J

jstorta

I am new to java as well.

I think your classpath setting goes one level too deep. It should be
set to the directory where the first level of your package sits.

C:\Java\Packages\Geometry\Shap­e2D>javac -classpath
".;C:\Java\Packages" Rectangle.java
 
H

Hemal Pandya

shaun said:
Hi,


As you can probably tell I am a beginner so any tips here would be
greatly appreciated.

Tip 1: The right place to ask beginner questions is the newsgroup
comp.lang.java.help.

Tip 2: You need to have classes Line and Point declare themselves to be
in package Geometry (in their respective .java files) and "import
Geometry.Line;" and "import Geometry.Point;" in Rectange.java.
 
R

Roedy Green

As you can probably tell I am a beginner so any tips here would be
greatly appreciated.

Don't use names for classes that are already being used by the tools
you are using. It is possible to do that, but for a beginning it just
introduces confusion.


Also post your complete program.

It will speak much more eloquently than you what your problem is.

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
H

HalcyonWild

shaun said:
Hi,
C:\Java\Packages\Geometry

In this directory I have 2 classes:

Line.java
Point.java

These classes have compiled correctly. I have created a directory in
geometry called Shape2D and in this directory created a file called
Rectangle.java, here are the contents:

package Geometry.Shape2D;

public class Rectangle {
// Data members
private Point topLeft;
private Point bottomRight;

// Create a rectangle from two points
public Rectangle(final Point topLeft, final Point bottomRight) {
this.topLeft = new Point(topLeft);
this.bottomRight = new Point(bottomRight);
}
}

When I try to compile this program i get the foillowing error:
....
As you can probably tell I am a beginner so any tips here would be
greatly appreciated.

Shaun


1. You might need to set classpath to include c:\Java\Packages. Better
set the environment variable from My Computer itself. Or, in *nix, set
the environment variable using export. If you do
set classpath=whatever;anything;.;c:\java\packages
in your current window, they might be lost when you open the window
again.
2. import the Line and Point classes. import Geometry.Line; import
Geometry.Point;
3. As Roedy said, try using different class names than what Java api
uses, try naming it ShaunsLine or MyLine etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top