Sign in to follow this  
HelloSkitty

[java] Circular Packages

Recommended Posts

HelloSkitty    152
So I know that circular classes are possible, for example

class ListNode {
ListNode next;
//some variables
//some methods
}

Earlier, I tried writing code that essentially did this:

package P1;
import P2.B;
class A {
//etc,etc.
}

and

package P2;
import P1.A;
class B {
//etc,etc.
}

Compiler error. Is there any way to make packages have circular references, or should I just put classes A and B in the same package if they are co-dependent?

Share this post


Link to post
Share on other sites
HelloSkitty    152
The error was

M.java:3: package D does not exist
import D.*;
^

My file structure is a folder called GAME. GAME has two folders, called D and M, my two package names. D has 2 classes, D and S, and an interface I. M has two classes C and M.

I wrote D, S, and I first. D is supposed to be a super-class of M. So M looks like:

[code]

package M;

import D.*;
import java.util.ArrayList;
import java.awt.Graphics;

public class M extends D implements I {

public ArrayList<C> C=new ArrayList<C>();

public M() {
int[] x={395,405,405,395};
int[] y={395,395,405,405};
int n=4;
C m=new C(x,y,n);
m.a(true);
m.t(true);
C.add(m);
}

public void d(Graphics g) {
for (C c:C) {
c.d(g);
}
}

public void l(int n) {
for (C c:C) {
c.l(n);
}
}

}
[/code]


I think I once used circular classes in eclipse before, and it worked okay, but after getting a new computer which I can't run eclipse on, I need to use command line to compile. So is there a way I would need to rearrange my file format for it to work?

P.S. Don't mind the one letter package, class, method, and variable names. And lack of comments. I can guarantee that it isn't the source of the problem. (At least, I hope not.)

Story: I was challenged to write a game using only one letter everythings and no commenting allowed, with exception to imported classes and main. I realized 26 classes was too restrictive, so I made packages. Then I ran into the circular package problem. [img]http://public.gamedev.net/public/style_emoticons/default/tongue.gif[/img]

Share this post


Link to post
Share on other sites
rip-off    10976
You need to ensure you are in the right directory. The javac compiler treats packages as directories. If you go to the directory GAME, and compile using something like javac M/M.java, it should work.

Share this post


Link to post
Share on other sites
Glass_Knife    8636
[quote name='rip-off' timestamp='1312498610' post='4844760']
You need to ensure you are in the right directory. The javac compiler treats packages as directories. If you go to the directory GAME, and compile using something like javac M/M.java, it should work.
[/quote]

Second this! In the past, I've had problems with having two different libraries, each of which reference each other. You can still make it work, but when
you move to another machine and try to compile from scratch it won't work. But moving them to the same library has always worked, so I think you
may not be doing the packages by hand correctly.

Share this post


Link to post
Share on other sites
HelloSkitty    152
Okay, now when I simul-compile (I was originally going into the two packages and compiling separately, thanks for advice!), it works, but when I try to run the main class S in package D, I get:

Exception in thread "main" java.land.NoClassDefFoundError: S (wrong name D/S)
etc.
etc.
.
.
.
Could not find the main class : S. Program will exit.

My compile and run batch file is this:

[code]
@echo off
PATH C:\glassfish3\jdk\bin

javac D\*.java M\*.java
pause

cd D
java S
pause
[/code]

I was pretty sure the error meant I should change java S to java D/S, but when I do that, I gives the same NoClassDefFoundError D/S, without any wrong name inside parenthesis. Is the problem just that my main class is inside a package? I initially meant for D to stand for Defaultpackage, so I can move everything inside it to the main directory, but that would make the file organization look awkward... is there a problem with the batch file, or does the main class need to be outside of my packages?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this