Kipt Scriddy
Kipt Scriddy

Reputation: 763

Java file not compiling

Currently trying to work with objects in Java. Everything goes fine until I hit compile. Have been reading a couple of other questions regarding the same problem, or the same given error, and at this point I am not sure wether I am forgetting something or that I need to change my classpath.

Main Class file:

package TesterClass;

public class Tester {
    public static void main(String[] args){

        TesterClass firstTest  = new TesterClass();
        firstTest.stringPrinter();
    }
}

The file that is supposed to be functioning as a package file:

package TesterClass;

public class TesterClass{

    private String workingSegment;

    public TesterClass(){
        workingSegment = "Working";
    }

    public void stringPrinter(){
        System.out.println(workingSegment);
    }
}

The 2 files are in the same directory and I am trying to manually compile them with "javac Tester.java". The error I get is about the fact that its having issues with the package. All help is welcome!

EDIT: Forgot to post the actual compiler error.

Tester.java:9: cannot find symbol
symbol  : class TesterClass
location: class TesterClass.Tester
        TesterClass firstTest;
        ^
Tester.java:11: cannot find symbol
symbol  : class TesterClass
location: class TesterClass.Tester
            firstTest = new TesterClass();
                        ^
2 errors

Upvotes: 1

Views: 6219

Answers (4)

MadProgrammer
MadProgrammer

Reputation: 347314

Move to the top of the source tree and compile both class...

So, assuming you source files are in \Java\TesterClass, you need to start in \Java

javac TesterClass\Tester.java TesterClass\TesterClass.java

You may also want to have a quick read of Code Conventions for the Java Programming Language as packages names are suppose to be in lower case :P

Updated

I just tried...

javac TesterClass\Tester.java

And it worked fine.

Are you sure that the Tester.java and TesterClass.java are in the TesterClass directory?

Updated with running example

So, basically, I dropped you .java files into the directory \compile under the TesterClass (\compile\TesterClass) directory and compiled them using...

\compile>javac TesterClass\Tester.java

Then I run them...

\compile>java TesterClass.Tester
Working

Upvotes: 3

user2888899
user2888899

Reputation: 21

Try changing the package name so it does not match the class name. Right now they are the same. Make it package TesterClassPackage, then import TesterClass into the file with the main() method. Even though they are in the same package sometimes you need to literally import files even though they are in the same package.

Upvotes: 1

AJJ
AJJ

Reputation: 3628

javac TesterClass\TesterClass.java TesterClass\Tester.java

will do it

Upvotes: 0

Rahul
Rahul

Reputation: 45070

You need to go to the top of the directory hierarchy and first compile your TesterClass and then compile your Tester. Since you have not compiled your TesterClass yet, Tester is unable to find it.

The error clearly states that its not able to find the symbol TesterClass, and the reason being TesterClass hasn't been compiled yet.

I suggest you use an IDE which does the compilation automatically for you. If you stick to manual compilation, you need to compile all the classes in the proper order.

Upvotes: 1

Related Questions