Packages?

On several different occasions, I have seen a good reason to put some classes into a package so that I can refer to them from other projects without having to copy them into the project itself, but here’s the thing: it NEVER works. I’ve tried every permutation of package name/folder hierarchy that I can think of, and I always run into a problem somewhere that makes the package unusable. I am sure this is simple, every tutorial and guide I’ve read is simple, but I need some help. Is there a caveat that I am unaware of?

My current attempt goes as follows (top folder is pack b/c I ended up moving it to a test folder):
folder: pack
—folder: drawable
------DrawableInt
------DrawableGrid
------…
------DrawableObject

They all belong to the package pack.drawable, and the other classes extend DrawableObject. The current problem is that the subclasses cannot find DrawableObject as soon as I add the package, even though I have the . in my classpath.

So which guru sees the blatant error I’m making? ::slight_smile:

Post the error message and the code here.

Is the library’s jar file in the classpath?

Thanks for replying. You may have just uncovered my problem. I’ve seen a jar file used once to transport a big project in a single file, and that’s about as far as my knowledge about them goes. None of the packaging tuts I’ve managed to find mentioned needing them anywhere, but I have no doubt that this fact means nothing. So, whats the run-down of a jar file?

DrawableObject


package pack.drawable;
import java.awt.*;
public abstract class DrawableObject
{
   protected int type;
   protected int x;
   protected int y;
   protected int offX;
   protected int offY;

   public DrawableObject(int type, int x, int y)
   {
      this.type = type;
      this.x = x;
      this.y = y;
      offX = 0;
      offY = 0;
   }
   public void setOffset(int nX, int nY)
   {
      offX = nX;
      offY = nY;
   }
   public void addOffset(int nX, int nY)
   {
      offX+= nX;
      offY+= nY;
   }
   public int drawX()
   {
      return x+offX;
   }
   public int drawY()
   {
      return y+offY;
   }
   public abstract void draw(Graphics2D g);
}

DrawableInt (all other subclasses are similar)


package pack.drawable;
import java.awt.Color;
import java.awt.Graphics2D;
public class DrawableInt extends DrawableObject
{
   int value;
   public DrawableInt(int type, int x, int y, int value)
   {
      super(type, x,y);
      this.value = value;
   }
   public void set(int value)
   {
      this.value = value;
   }
   public void add(int amount)
   {
      value += amount;
   }
   public int get()
   {
      return value;
   }
   public void draw(Graphics2D g)
   {
      g.setColor(ColorPalette.getColor(type));
      g.drawString(value+"",x+offX,y+offY);
   }
}

Oh, and the long list of error messages all relate to the fact that the subclasses can’t find their parent, starting with:


>javac DrawableInt.java
DrawableInt.java: 4: cannot find symbol
symbol: class DrawableObject
public class DrawableInt extends DrawableObject

If you want DrawableInt to use the package pack.drawable and not be in that package you need to change

package pack.drawable;

to

import pack.drawable.DrawableObject;

I changed DrawableInt to


import pack.drawable.DrawableObject;
import pack.drawable.ColorPalette;
import java.awt.Color;
import java.awt.Graphics2D;
public class DrawableInt extends DrawableObject
{
   int value;
   public DrawableInt(int type, int x, int y, int value)
   {
      super(type, x,y);
      this.value = value;
   }
   public void set(int value)
   {
      this.value = value;
   }
   public void add(int amount)
   {
      value += amount;
   }
   public int get()
   {
      return value;
   }
   public void draw(Graphics2D g)
   {
      g.setColor(ColorPalette.getColor(type));
      g.drawString(value+"",x+offX,y+offY);
   }
}

Thanks

It now runs when I place DrawableInt outside of the pack folder. Is this what you meant? Is there some way for them to be in the same package (or in a hierarchy within the package) and work together? I’m sorry, this is just something I could never figure out.

What is your directory structure and how do you compile the files? javac must be run from the root folder: javac pack\drawable*.java
(see the attached file - rename it to .zip because the forum did not allow uploading .zip)

Usually it’s the easiest to use an IDE (or Maven/Ant) to compile the files when many jars and packages are involved. It’s good to know how to do it on the command line, but for any real work the command line is too cumbersome.

This is really strange, I took the time to remake and test the package in and its exactly the same as the way I did (and the obvious way I ALWAYS try to set it up), but here’s the thing: it works in Eclipse fine in eclipse, but even if I move the files directly over, command prompt still cant find DrawableObject to make subclasses.

I’m left to believe there is something amiss in my classpath, perhaps Eclipse avoids the problem with that .classpath file…

CLASSPATH: .;C:\Documents and Settings\Jon\My Documents\android\tools;C:\Program Files\Java\jre1.6.0_03\lib\ext\QTJava.zip
PATH: .;C:\Documents and Settings\Jon\My Documents\android\tools;C:\Program Files\Java\jre1.6.0_03\lib\ext\QTJava.zip

Any ideas?

P.S. The files are in the same folder \drawable\objects\

When you move the files over, do you retain the directory structure? The directory structure is the package structure and it important. You should have a dir for your project and under that should be your package directories
/projectDir/pack/drawable
and from within the /projectDir you would issue
javac -cp ./ pack/drawable/DrawableInt.java
in order to compile the file as part of your package.

If you just move all the files to /projectDir without the directory structure, the package files will no longer compile.

Oh My God! Thank you! You have no idea how many hours I’ve wasted on that! All caused by the -cp option.

So, just so I’m clear. In order for a class within the package to find other classes in the package you have to send it the classpath of where the pack begins? ie:

I have DrawableInt in the package drawable.objects that extends DrawableObject (also within the package)

So it wants to look into its package drawable.objects for its parent. In order to find it, it has to start from the beginning of the package hierarchy (project folder) and make its way back to its package drawable.objects? And, of course, the only way it can find anything is through the classpath.

Can I has a Cookie? =D

Now to get back to what I was working on before I thought “wait, it would be easier to put this in a package” a few days ago, lol.