I am working through a practice assignment in my Java Programming Textbook and have encountered the common "error: cannot find symbol". I have to create an application that refers to another class in the same directory, but the compiler fails to find the class.
Here is the code for SpaService.java:
package com.spaservice;
public class SpaService {
private String serviceDescription;
private double price;
public void setServiceDescription(String service){
serviceDescription = service;
}
public void setPrice(double servicePrice){
price = servicePrice;
}
public String getServiceDescription(){
return serviceDescription;
}
public double getPrice(){
return price;
}
}
And here is my code for CreateSpaServices.java
package com.spaservice;
import java.util.Scanner;
public class CreateSpaServices {
public static void main(String args[]) {
String service;
double price;
SpaService firstService = new SpaService();
SpaService secondService = new SpaService();
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter service >> ");
service = keyboard.nextLine();
System.out.print("Enter price >> ");
price = keyboard.nextDouble();
firstService.setServiceDescription(service);
firstService.setPrice(price);
keyboard.nextLine();
System.out.print("Enter service >> ");
service = keyboard.nextLine();
System.out.print("Enter price >> ");
price = keyboard.nextDouble();
secondService.setServiceDescription(service);
secondService.setPrice(price);
System.out.println("First service details:");
System.out.println(firstService.getServiceDescription() +
" $" + firstService.getPrice());
System.out.println("Second service details:");
System.out.println(secondService.getServiceDescription() +
" $" + secondService.getPrice());
}
}
It's a fairly straightforward program, but for some reason CreateSpaServices.java cannot find SpaService.class. Here is the output from my command prompt in Windows:
> C:\Users\waxyshaw\Desktop\School\CH3-EX11\SpaService\src\main\java\com\spaservice>java
> CreateSpaServices.java CreateSpaServices.java:18: error: cannot find
> symbol
> SpaService firstService = new SpaService();
> ^ symbol: class SpaService location: class CreateSpaServices CreateSpaServices.java:18: error: cannot find symbol
> SpaService firstService = new SpaService();
> ^ symbol: class SpaService location: class CreateSpaServices CreateSpaServices.java:19: error:
> cannot find symbol
> SpaService secondService = new SpaService();
> ^ symbol: class SpaService location: class CreateSpaServices CreateSpaServices.java:19: error: cannot find symbol
> SpaService secondService = new SpaService();
> ^ symbol: class SpaService location: class CreateSpaServices 4 errors error: compilation failed
And here is my directory structure:
C:\Users\waxyshaw\Desktop\School\CH3-EX11\SpaService\src\main\java\com\spaservice>dir
Volume in drive C has no label.
Volume Serial Number is 9A2C-802D
Directory of C:\Users\waxyshaw\Desktop\School\CH3-EX11\SpaService\src\main\java\com\spaservice
05/29/2021 17:26 <DIR> .
05/29/2021 17:26 <DIR> ..
05/29/2021 17:28 1,418 CreateSpaServices.java
05/29/2021 17:25 590 SpaService.java
2 File(s) 2,008 bytes
2 Dir(s) 609,747,128,320 bytes free
I am using Netbeans 12.3 to write this code and it compiles fine using the IDE. I am on Windows 10.
Based on research, I have seen similar issues here on Stack Overflow. I have tried running the command from my src folder, my java folder, and my com folder with similar results. I suspect the issue may have to do with the package, but I do not yet understand Java enough to troubleshoot on my own. I'm hoping I can get some help from the community.
Let me know what you think. Any help would be greatly appreciated.
Edit: Per request, I have included a screenshot of the error:
Edit2: Including a screenshot of the output window in NetBeans:
I managed to get it to work. Like in many other posts on here, you have to compile from the root directory. NetBeans makes it confusing by creating so many directories when you first start a project.
My package was com.spaservice. I had to compile the class from the src\main\java folder. Then the java file was able to find the other class during compilation.