• Advertisement

# [java] Circular Packages

This topic is 2391 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

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

##### Share on other sites
Advertisement
What error are you getting? You should be able to set this up - though it is unusual and I wouldn't recommend it if it can be avoided.

#### Share this post

##### Share on other sites
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:

 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); } } } 

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.

#### Share this post

##### Share on other sites
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

##### Share on other sites

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.

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

##### Share on other sites
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:

 @echo off PATH C:\glassfish3\jdk\bin javac D\*.java M\*.java pause cd D java S pause 

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

##### Share on other sites
When running from the command line, pretty much the same rules apply. Start in the super-directory and use java D.S.

#### Share this post

##### Share on other sites
Aah! It works! That was a rather quick reply too... only two minutes. Thanks for the help!

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement
• ### Popular Tags

• Advertisement
• ### Popular Now

• 10
• 18
• 11
• 10
• 12
• Advertisement