0

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:

Error-Screenshot

Edit2: Including a screenshot of the output window in NetBeans:

Netbeans-Output

Waxyshaw
  • 3
  • 5

2 Answers2

0

The command java CreateSpaServices.java you used to execute your program is not meant to be used for programs with multiple source files. It is used to executed single source file java programs without compilation. see JEP330 for more details.

Normally you'd write your Java code, compile it (javac) and afterwards run it (java). You can use the manually or use editor like NetBeans.

So the command you showed above indicates that you use NetBeans just as Editor and don't run nor use the compilation from NetBeans. Which is unclear why.

Normally you'd use NetBeans as your editor and compiler. Means that you write your code within NetBeans and after that you create a jar or class files which can be executed from command line.

When you just want to run the program you can Right Click CreateSpaServices \ Run File to execute the Main method of CreateSpaServices.

asbachb
  • 543
  • 3
  • 17
  • Thanks for the reply. Does that mean I should configure the multiple classes in the same .java file instead of having two separate files? – Waxyshaw May 29 '21 at 23:42
  • I tried adding the class from the SpaService.java code into my CreateSpaServices.java class, but it just gives me another error: Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: com.spaservice.SpaService" – Waxyshaw May 30 '21 at 03:00
  • This does not address the question at all. The OP is asking about an error when running an application that was compiled in NetBeans from the command line. Running the application within NetBeans is irrelevant. – skomisa May 31 '21 at 17:34
  • Sorry to disagree. But when you check which command was called you see that the OP tries to execute a Java source file not anything compiled via NetBeans. As I answered you can run source files only when it is a single source file specified in JEP330. In order to ease the live of op I suggested to run that stuff via NetBeans and not via command line for now. – asbachb Jun 01 '21 at 13:04
  • Hi asbachb, I have managed to get the code to at least compile in Netbeans, but when I try to run the code it hits 50% and doesn't execute any of the logic like prompting the user to "Enter service". – Waxyshaw Jun 02 '21 at 20:59
  • @Waxyshaw Have you opened the window "Output"? Normally the text should be print there. – asbachb Jun 04 '21 at 00:10
  • Yes. I'll attach a screenshot of the output as well so you can see what I'm talking about – Waxyshaw Jun 04 '21 at 13:31
0

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.

Waxyshaw
  • 3
  • 5