javaobjectoopmethodscannot-find-symbol

Why am I seeing the "cannot find symbol error" in java when creating an object from another class?


This is my primary class named Revenue where I'm trying to call a secondary class in same directory as this one and in the same package as well

package Hotel;

//import java.util.*;

public class Revenue {
    String Name;
    
    Revenue()
    {
        Name="";
    }
    public void Guest() 
    {
        
        System.out.println("Revenue Guest. ");
        Persons x = new Persons(); //Here's where the error is actually occuring
        System.out.println("Guest name:" + x.name() );  
        
    }
    public static void main(String args[])
    { 
        System.out.println("Revenue main.");
        Revenue rev = new Revenue();
        rev.Guest();
    }
}

Now the next part is secondary class

package Hotel;

public class Persons {
    String firstName[] = {"Adam","Alex", "Steve", "Sira"};
 
    public Persons()
    {

    }
    public String name() {
        System.out.println("Persons name");
        java.util.Random r = new java.util.Random();
        String name = firstName[r.nextInt(firstName.length)];
        return name; 
    }
    public static void main(String[] args)
    {
        Persons n = new Persons();
        System.out.println(n.name());
    }
}//class

And yes it works fine on debug mode through vs code (Redhat java something). But when i try to do javac Revenue.java or java Revenue.java in cmd it shows an error like this.

Revenue.java:18: error: cannot find symbol
        Persons x = new Persons();
        ^
  symbol:   class Persons
  location: class Revenue
Revenue.java:18: error: cannot find symbol
        Persons x = new Persons();
                        ^
  symbol:   class Persons
  location: class Revenue
2 errors
error: compilation failed

It's been whole 24 hours but I'm not able to figure it out what's the issue here :(

I tried creating a program named revenue that will extract a persons name from another class named person(Just for the sake of future expansion of person class). And in BlueJ it seemed to work pretty fine without any error but recently I switched to cmd and vs code and here I'm stuck with this code where it wont let me call a method from another class.


Solution

  • Just try the command java className.java to compile and execute like java Persons.java. It should work.

    enter image description here