0

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.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • go to the parent of the "hotel" folder. Then `javac hotel/Revenue.java' – Seb Perp Jun 05 '23 at 19:27
  • The Java compiler uses classes' fully-qualified names to find them or their sources. The package name maps to a directory tree, and the class name to a filename. To compile from the command line, make sure that the directory containing your two sources is named `Hotel`, and *from its parent directory*, run `javac Hotel/Revenue.java`. – John Bollinger Jun 05 '23 at 19:28
  • I tried this method and then tried java Hotel/Revenue.class and java Revenue.class (going into hotel directory). Both showed an error java .\Revenue.class Error: Could not find or load main class .\Revenue.class Caused by: java.lang.ClassNotFoundException: /\Revenue/class – user9812664 Jun 05 '23 at 19:37
  • Having successfully compiled, you *run* it via its fully-qualified **class** name. Supposing you're still in the same directory from which you compiled, the command would be `java Hotel.Revenue`. – John Bollinger Jun 05 '23 at 19:43
  • *Tangential: If `Persons` represents a single person (which it appears to) it should be named `Person` (singular) to avoid confusion.* – Dave Newton Jun 05 '23 at 19:44
  • Thank you for all the replies. Somehow John this java Hotel.Revenue thing worked and it's working perfectly fine! Thanks for the help. Although it's still a little confusing to me how it's making a difference – user9812664 Jun 05 '23 at 19:48
  • Yeah. I felt that Persons should be renamed but just for the sake of not getting an error due to any misspelt word, I avoided that risk. – user9812664 Jun 05 '23 at 19:49
  • And the `Hotel` package should be named `hotel` ... to conform with Java style rules. – Stephen C Jun 06 '23 at 00:32

1 Answers1

0

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

enter image description here