Package question

Hi
This isn’t specific to games but hopefully someone might be able to point me in the correct direction.
I have 1 main class which imports a package containing 2 smaller classes (TestClassOne and TestClassTwo).
TestClassOne has to create an instance of TestClassTwo, but when compiling TestClassOne I keep getting the error “Unable to find symbol” in reference to TestClassTwo.

Both test classes are in the dir …org/testpackage/ which I’ve added to my classpath, I’ve also done something reasonably similar to this as part of a tutorial from a book which worked.

I’ve tried putting a little test program together with the bare minimum in it incase some of the other code was tripping me up but I’m still getting the same thing. Does anyone have any ideas please? :-\


package org.testpackage;
import java.io.*;

public class TestClassOne{

	public TestClassOne(){
		super();
		doStuff();
	}

	public static void doStuff(){
		TestClassTwo tempTestClass = new TestClassTwo();
	}

}


package org.testpackage;

public class TestClassTwo{

	public TestClassTwo(){
		super();
		doStuff();
	}

	public void doStuff(){
		System.out.println("I work!");
	}

}

Well there’s nothing wrong with this - if they’re in the same package as you’ve described then you shouldn’t have to do an import.

There must be more to this than what you’ve put in the forum. Look for simple spelling errors, missing semicolons or things like that. But probably there’s some corrupted folder in your project or something. What IDE are you using?

I copied your TestClassOne.java and TestClassTwo.java and added my own Main.java and tried it from the command line:

org\mainpackage\Main.java:

package org.mainpackage;

import org.testpackage.TestClassOne;

public final class Main {
	public static void main(String[] args) {
		System.out.println("Here is a TestClassOne I just created: " + new TestClassOne());		
	}
}
C:\Users\irr\Desktop\jgo>dir *.java /S /B
C:\Users\irr\Desktop\jgo\org\mainpackage\Main.java
C:\Users\irr\Desktop\jgo\org\testpackage\TestClassOne.java
C:\Users\irr\Desktop\jgo\org\testpackage\TestClassTwo.java

C:\Users\irr\Desktop\jgo>javac org\mainpackage\Main.java

C:\Users\irr\Desktop\jgo>java -cp . org.mainpackage.Main
I work!
Here is a TestClassOne I just created: org.testpackage.TestClassOne@addbf1

Hi.
I’m just using notepad and the command line to develop this.

Thanks.

Well then are sure that both of these classes are being compiled and are in the classpath?

Hi
Thanks for having another look at this.

TestClassTwo compiles without error.
TestClassOne gives the error:


Cannot find symbol
Symbol: TestClassTwo
Location: org.testpackage.TestClassOne
 TestClassTwo tempTestClass = new TestClassTwo();
 ^

Cannot find symbol
Symbol: TestClassTwo
Location: org.testpackage.TestClassOne
 TestClassTwo tempTestClass = new TestClassTwo();
                                                           ^

Edit: the ^ should be under the T’s in TestClassTwo

Classpath variable is set to : c:\javacc-5.0\bin;f:\users\jp\appdata\java\org.testpackage;
Added through the windows control panel

My files are in the directory : f:\users\jp\appdata\java\org.testpackage\

strange do you compil both at the same time like this : javac file1.java file2.java

Bingo! ;D
I was compiling them separately as I wasn’t aware they would have to be done at the same time (I assume they have to be done at the same time as it works now).
Thanks very much for all who posted.
Feel a bit of a berk for tearing my hair out over this.

Your classpath and folders are “wrong”, too. Each package segment has to have a separate folder, so its f:\users\jp\appdata\java\org\testpackage\ and the classpath always has to point to the root-package, so it should be f:\users\jp\appdata\java\ without any package name in there.

Actually you deserve a compliment for starting out on the lowest level, without and IDE and compiling everything yourself. That said, I do hope that you eventually switch to Eclipse or Netbeans, because you really don’t want to use Notepad for programming :slight_smile:

Thanks for the tips peeps. I’ll alter my classpath just now.
I find notepad a bit simpler to use, that said once I get my feet wet I’ll prolly have to spend time getting comfortable with eclipse. Especially once I try to make something of any real size.

You may like notepad++ instead, so you have syntax coloration.

And if you want to play a bit, you could try to automate the stuff you’ve learned about the java tools like this:

Notepad++ Rules…!
I use it when i program.

Ah cool. Ty.
I use notepad at work, hadn’t thought of using it to compile java code in though :slight_smile: