1

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
9
  • Can you paste the error message or stacktrace here? Commented Oct 17, 2013 at 3:56
  • Please post the exact error message. Commented Oct 17, 2013 at 3:56
  • 2
    Move back to the top of the package branch (..) and try recompiling using javac TesterClass/Tester.java Commented Oct 17, 2013 at 3:56
  • Added the compiler error, sorry my bad! Commented Oct 17, 2013 at 3:59
  • As @MadProgrammer says -- compile from the root of your source-folder, if you try to compile from "within" a package then javac doesn't understand your package structure. Package names are also meant to be lowercase by convention & should not just mimic the classnames. Try "scratch", "test", "work" as package names. Real systems use package names like "myAccounts.customer", "myAccounts.product", "myAccounts.order" or perhaps "myPDF.reader", "myPDF.display" or perhaps "game.monsters", "game.map", "game.player". Commented Oct 17, 2013 at 4:01

4 Answers 4

3

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
Sign up to request clarification or add additional context in comments.

14 Comments

Currently both files compiled successfully. Only problem now is that I am getting an error when I am trying to run the file
What exact error do you get when you try to execute your Tester class?!
It is telling me "Exception in thread "main" java.lang.NoClassDefFoundError: Test/Tester (wrong name: TesterClass/Tester)"
@KiptScriddy Test/Tester isn't where the class files exist, they should exist in Tester. Take a look at the updates
Why isnt it working when I try to run it in the folder itself as " java Tester"? Not really understanding where the problem atm is
|
1

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.

Comments

1

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.

Comments

0

javac TesterClass\TesterClass.java TesterClass\Tester.java

will do it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.