And then the package line goes...?

Ultimate Newless Clubie question here:

Trying to make a package, can’t do it, and yes i have tried searching ???

I have two basic classes with just a constructor and the following line at the top:

package RandomPackage;

They are contained within a folder called RandomPackage.

I have my test file in the folder that contains the RandomPackage folder. I have imported the package using:

import RandomPackage.*;

Up to this point everything works, the Tester can create instances of the two basic classes within the package RandomPackage.
The problem is that the two basic classes cannot refer to each other. I cannot create an instance of basic class A within basic class B… This pretty much ruins the whole point of packaging so I must be doing something wrong.
How do I fix this?

Thank you in advance

Simon

[quote]The problem is that the two basic classes cannot refer to each other. I cannot create an instance of basic class A within basic class B… This pretty much ruins the whole point of packaging so I must be doing something wrong.
How do I fix this?
[/quote]
Post one of the code attempts so we can see what you’re doing. Once we have a basis to work from, we can give you some advice. :slight_smile:

Sorry about the lack of code.

The class that runs it all:

import RandomPackage.*;

public class TESTER1 {

  public static void main(String[] args){

        TestClass2 t = new TestClass2();  [Edit = I forgot the 2 in the TestClass2  :o]

  }

}

The two test classes, In two seperate java files:

package RandomPackage;

public class TestClass{

  public TestClass(){
        System.out.println("Created Test Class");
  }

}

package RandomPackage;

public class TestClass2{

  TestClass insideClass;

  public TestClass2(){
        insideClass = new TestClass();
  }

}

Are you getting errors? When you compile or when you run? I suspect this might be a simple classpath issue. Here is a sample error you might get when it is a classpath issue:

C:\Test\Java\work\RandomPackage\TestClass2.java:5: cannot resolve symbol
symbol  : class TestClass 
location: class RandomPackage.TestClass2
 TestClass insideClass; 
 ^
C:\Test\Java\work\RandomPackage\TestClass2.java:8: cannot resolve symbol
symbol  : class TestClass 
location: class RandomPackage.TestClass2
  insideClass = new TestClass(); 
                    ^
2 errors

If this is your problem, then you simply have to set the classpath to the directory where RandomPackage resides. You can do this with an environment variable or the ‘-classpath’ command line switch. Then the javac command must either be executed in the directory where RandomPackage is or you can use the ‘-sourcepath’ switch to point to it. So if your directory structure is:

c:\java\work\RandomPackage

you need to do this:

c:\java\work>javac -classpath . TESTER1.java RandomPackage/*.java

If this is not your problem, you need to provide more information. What you are trying to do(compile, execute) and what your error messages are.

You have no line where you are creating a: new TestClass2()

In your main change :

new TestClass()

to

TestClass2 t2 = new TestClass2();

Then TestClass2 will make a new TestClass and print the result
You should put a print() in TestClass2 also so you know they were both touched.

[quote]Are you getting errors? When you compile or when you run? I suspect this might be a simple classpath issue. Here is a sample error you might get when it is a classpath issue:

C:\Test\Java\work\RandomPackage\TestClass2.java:5: cannot resolve symbol
symbol  : class TestClass 
location: class RandomPackage.TestClass2
 TestClass insideClass; 
 ^
C:\Test\Java\work\RandomPackage\TestClass2.java:8: cannot resolve symbol
symbol  : class TestClass 
location: class RandomPackage.TestClass2
  insideClass = new TestClass(); 
                    ^
2 errors

If this is your problem, then you simply have to set the classpath to the directory where RandomPackage resides. You can do this with an environment variable or the ‘-classpath’ command line switch. Then the javac command must either be executed in the directory where RandomPackage is or you can use the ‘-sourcepath’ switch to point to it. So if your directory structure is:

c:\java\work\RandomPackage

you need to do this:

c:\java\work>javac -classpath . TESTER1.java RandomPackage/*.java

If this is not your problem, you need to provide more information. What you are trying to do(compile, execute) and what your error messages are.
[/quote]
Firstly, thanx!
This is exactly my problem.;D

Unfortunately I still cant get it working :-[

How do I compile the classTestClass2.

I recieve the error:

D:\My Programs\Tests\RandomPackage\TestClass2.java:5: cannot resolve symbol
symbol : class TestClass
location: class RandomPackage.TestClass2
TestClass insideClass;
^
D:\My Programs\Tests\RandomPackage\TestClass2.java:8: cannot resolve symbol
symbol : class TestClass
location: class RandomPackage.TestClass2
insideClass = new TestClass();
^
2 errors

Tool completed with exit code 1

I tried c:\java\work\RandomPackage>javac -classPath . TestClass.java RandomPackage/*.java

but that does not work…

One more question , I’m currently using textpad to write my java code, which means your solution requires I either:
compile through command prompt (which is annoying)
create a new user defined tool that compiles and includes the extra arguments “c:\java\work>javac -classpath . TESTER1.java RandomPackage/*.java”
Is there another way that I have missed?
This whole package thing seems rather complicated for something that should be so simple :-/

Thanx again

Simon

Don’t forget, everything is case sensitive. You need to use ‘-classpath’ not ‘-classPath’