Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (482)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (548)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Packages?  (Read 2244 times)
0 Members and 1 Guest are viewing this topic.
Offline Del-ONE

Senior Newbie





« Posted 2008-09-23 14:28:20 »

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?  Roll Eyes
Offline Jackal von ÖRF

Junior Member





« Reply #1 - Posted 2008-09-23 14:33:12 »

Quote
The current problem is that the subclasses cannot find DrawableObject as soon as I add the package

Post the error message and the code here.

Quote
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.

Is the library's jar file in the classpath?

Offline Del-ONE

Senior Newbie





« Reply #2 - Posted 2008-09-23 19:27:17 »

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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
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)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
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:

1  
2  
3  
4  
>javac DrawableInt.java
DrawableInt.java: 4: cannot find symbol
symbol: class DrawableObject
public class DrawableInt extends DrawableObject
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Wildern

Junior Member





« Reply #3 - Posted 2008-09-23 19:42:00 »

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

to
1  
import pack.drawable.DrawableObject;
Offline Del-ONE

Senior Newbie





« Reply #4 - Posted 2008-09-23 21:58:56 »

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

to
1  
import pack.drawable.DrawableObject;


I changed DrawableInt to
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
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.
Offline Jackal von ÖRF

Junior Member





« Reply #5 - Posted 2008-09-24 08:51:01 »

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.

Offline Del-ONE

Senior Newbie





« Reply #6 - Posted 2008-09-25 22:33:59 »

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 <path>\drawable\objects\
Offline Wildern

Junior Member





« Reply #7 - Posted 2008-09-25 22:42:16 »

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.
Offline Del-ONE

Senior Newbie





« Reply #8 - Posted 2008-09-26 01:57:50 »

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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (27 views)
2014-08-19 09:29:53

Tekkerue (25 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (15 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (61 views)
2014-08-11 02:49:23

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!