Casey Phillips
Casey Phillips

Reputation: 1

"error: cannot find symbol" while compiling; the class I am trying to use "cannot be found"

I have an assignment the requires an initial class and a separate project to test that class. Now, everytime i attempt to compile it it gives me a these errors:

package blooddata;

   TestBloodData.java:10: error: cannot find symbol
          BloodData patient1 = new BloodData("AB", "-");
          ^
      symbol:   class BloodData
      location: class TestBloodData
   TestBloodData.java:10: error: cannot find symbol
          BloodData patient1 = new BloodData("AB", "-");
                                   ^
      symbol:   class BloodData
      location: class TestBloodData
    TestBloodData.java:11: error: cannot find symbol
          BloodData patient2 = new BloodData("B", "+");
          ^
      symbol:   class BloodData
      location: class TestBloodData
     TestBloodData.java:11: error: cannot find symbol
          BloodData patient2 = new BloodData("B", "+");
                                   ^
       symbol:   class BloodData
       location: class TestBloodData
      4 errors

The actual code is below. Ive tried googleing, youtubing, and reading my textbook. I am so confused why it is saying this. Ive tried closing and reopening NetBeans and even uninstalling and installing the IDE, but nothing is apparently working. I've tried setting the main class in the project properties to the BloodData class i'm testing (which was most of the recommendations)but that didn't worked either.

package blooddata;

/**
 *
 * @author CaseyPhillips
 */
public class BloodData {

    public String bloodType;
    public String rHFactor;

    public BloodData() 
    {
        bloodType = "O";
        rHFactor = "+";
    }
    public BloodData(String bloodType, String rHFactor)
    {
        this.bloodType = bloodType;
        this.rHFactor = rHFactor;
    }
    public void setBloodType(String bloodType)
    {
        this.bloodType = bloodType;

    }
    public String getBloodType()
    {
        return bloodType;
    }
    public void setRHFactor(String rHFactor)
    {
        this.rHFactor = rHFactor;
    }
    public String getRHFactor()
    {
        return rHFactor;
    }

    public void showBloodType()
    {
        System.out.println("The patient's blood type is " + 
            bloodType + " and their Rh Factor is " + rHFactor);
    }

    public static void main(String[] args) {
        // TODO code application logic here
    }

}

Test Driver:

package blooddata;


/*
Author: Casey Phillips
*/
public class TestBloodData 
{
    public static void main(String[] args) 
    {
        BloodData patient1 = new BloodData("AB", "-");
        BloodData patient2 = new BloodData("B", "+");
        patient1.showBloodType();
        patient2.showBloodType();
    }
}

And the compile commands im using are:

Caseys-MacBook-Pro:~ CaseyPhillips$ cd NetBeansProjects/BloodData/src/blooddata
Caseys-MacBook-Pro:blooddata CaseyPhillips$ javac TestBloodData.java
TestBloodData.java:11: error: cannot find symbol
        BloodData patient1 = new BloodData("AB", "-");
        ^
  symbol:   class BloodData
  location: class TestBloodData
TestBloodData.java:11: error: cannot find symbol
        BloodData patient1 = new BloodData("AB", "-");
                                 ^
  symbol:   class BloodData
  location: class TestBloodData
TestBloodData.java:12: error: cannot find symbol
        BloodData patient2 = new BloodData("B", "+");
        ^
  symbol:   class BloodData
  location: class TestBloodData
TestBloodData.java:12: error: cannot find symbol
        BloodData patient2 = new BloodData("B", "+");
                                 ^
  symbol:   class BloodData
  location: class TestBloodData
4 errors

Upvotes: 0

Views: 3284

Answers (1)

h0r53
h0r53

Reputation: 3229

Since you are using packages you need to include your two classes BloodData and TestBloodData inside a folder called blooddata (which is named after your package).

Assume that the files BloodData.java and TestBloodData.java are inside of a folder called blooddata. From the parent folder, you should be able to compile your driver using javac.

javac blooddata/TestBloodData.java

This will resolve your linking error. To run the compiled class file, you can issue the following command also from the parent folder.

java blooddata.TestBloodData

Someone else can probably explain why packages must be managed in this manner better than I can, but I believe it has to do with how the java runtime environment (JRE) identifies classes that are part of a package. You cannot simply compile/execute from within the package folder itself. It seems that you need to compile/execute from a folder relative to the package path.

EDIT - To be as explicit as possible based on the updated compile commands and path information, you need to do the following:

$ cd NetBeansProjects/BloodData/src/
$ javac blooddata/TestBloodData.java

This should resolve your linking error. Then you can run the code from the same directory with:

$ java blooddata.TestBloodData

I know this may seem strange but the point is - when you are using Java packages you must execute commands relative to the package path, which in your case is the directory that includes blooddata, not the blooddata directory itself.

Upvotes: 0

Related Questions