0

I am following a Java tutorial (left to my own devices to write the test code), but when trying to compile I get a symbol not found error. I've looked and looked, but cannot work out why the code I have written produces this error. It's probably very simple, but I'd appreciate someone pointing out the cause as I'm pulling my hair out trying to understand what I've done wrong!

TestBeerExpert.java:

package com.example.model; import com.example.model.*; import java.util.*; public class TestBeerExpert { public static void main(String[] args) { TestBeerExpert test = new TestBeerExpert(); test.go(); } private void go() { BeerExpert expert = new BeerExpert(); List<String> brands = expert.getBrands("amber"); ... } } 

BeerExpert.java:

package com.example.model; import java.util.*; public class BeerExpert { public List<String> getBrands(String color) { List<String> brands = new ArrayList<String>(); ... return(brands); } } 

Directory structure:

beerV1 -> src -> com -> example -> model -> TestBeerExpert.java & BeerExpert.java

Compiling from beerV1 with javac -d classes src/com/example/model/TestBeerExpert.java

And the actual error:

src/com/example/model/TestBeerExpert.java:14: error: cannot find symbol BeerExpert expert = new BeerExpert(); ^ symbol: class BeerExpert location: class TestBeerExpert src/com/example/model/TestBeerExpert.java:14: error: cannot find symbol BeerExpert expert = new BeerExpert(); ^ symbol: class BeerExpert location: class TestBeerExpert 2 errors 

For the life of me I can't work out what I'm doing wrong. The files are in the same directory and package, so as far as I am aware this should be compiling. I'd be grateful to now only have the code corrected but and explanation of what I have done wrong so I can remember it for the future. Thanks in advance.

1
  • 3
    Have you compiled BeerExpert first? Commented Feb 8, 2013 at 19:55

2 Answers 2

4

Add src to your sourcepath

 javac -sourcepath src -d classes src/com/example/model/TestBeerExpert.java 

You need to do this because you execute javac from a different directory from where the sources are.

No need to compile BeerExpert first (javac will do it for you with the above command).

Validatation:

~/beerV1$ ls src/com/example/model/ BeerExpert.java TestBeerExpert.java ~/beerV1$ ls classes ~/beerV1$ javac -sourcepath src -d classes src/com/example/model/TestBeerExpert.java ~/beerV1$ ls classes/com/example/model/ BeerExpert.class TestBeerExpert.class 
Sign up to request clarification or add additional context in comments.

3 Comments

Why would one have the directory containing the .java files in the classpath?
Thanks, that works a treat. I would not have figured that out on my own. It also turns out that I need to specify the classpath when running the test also (that was another painful Internet search, you'd have thought I'd have cottoned on to that after the compiling issues...)! Much appreciated.
@RohitJain You were right. The correct thing is to add src to the sourcepath. Classpath also works, but it seems wrong conceptually. Updated answer.
0

You are getting the compile error because compiler is not able to find BeerExpert class. Try below 1. first compile BeerExpert.java using : javac -d classes src\com\example\model\BeerExpert.java 2. then compile TestBeerExpert.java javac -cp classes -d classes src\com\example\model\TestBeerExpert.java

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.