0

I am following along with JAVA book for CS101 and trying to wrap my head around this exercise, but when I run my code, I keep getting this error

Error: Could not find or load main class Riddle

Caused by: java.lang.ClassNotFoundException: Riddle

JAVA CODE

public class Riddle{
        private String question;        //instance variables
        private String answer;
    
        public Riddle( String q, String a)      //constructor
        {
            question = q;
            answer = a;
        }
    
        public String getQuestion()     //instance method
        {
            return question;
        }
    
        public String getAnswer()
        {
            return answer;
        }
}
    
public class RiddleUser{
        public static void main ( String argv [] ){
            Riddle riddle1= new Riddle (
                "What is black and white and red all over?",
                "An embarrassed zebra.");
            Riddle riddle2= new Riddle (
                "What is black and white and read all over?",
                "A newspaper." );
    
            System.out.println("Here are two riddles:");
            System.out.println(riddle1.getQuestion());
            System.out.println(riddle2.getQuestion());
            System.out.println("The answer to the first riddle is:");
            System.out.println(riddle1.getAnswer());
            System.out.println("The answer to the second riddle is:");
            System.out.println(riddle2.getAnswer());
       }
}

Here is the exercise I am doing, I also uploaded the PDF of the book, the exercise is on pages 71-73. Basically, it wants to show you how to write a class and test it, If I am getting this correctly.

enter image description here

enter image description here

enter link description here

crytpodoc
  • 31
  • 1
  • 9
  • 2
    You have two classes here, `Riddle` and `RiddleUser`. The code itself won't compile, so please tell us what's really going on. It's possible these classes are in two separate projects in your IDE, or they're in two separate packages and you forgot the `import` statement. Please add details to your question to explain where these classes actually are. – markspace Jul 22 '21 at 03:15
  • It seems like `Riddle` class does not have main method. – nkgcp Jul 22 '21 at 03:15
  • Is all code in one Java file? Only one `public class` is allowed in Java. – chenzhongpu Jul 22 '21 at 03:16
  • @nkgcp "ClassNotFoundException" probably is not caused by a missing main method. I think there's a different issue here. – markspace Jul 22 '21 at 03:17
  • [Edit] your question and post the **entire** stack trace - as text and not as a screen capture. – Abra Jul 22 '21 at 03:55

2 Answers2

4

On a Java file, only 1 class can be public, the public class is the one with the main method.

The file and the public class must have the same name, RiddleUser.java in this case.

0

After declaring RiddleUser class in its own file.

This should work if you add private scope modifier to the RiddleUser class, without declaring it in its own file aswell.

Say your source path folder is ~/riddle-project/src/

riddle-project
└── src
    ├── Riddle.java
    └── RiddleUser.java

Execute the following:

# compile main class
$ javac src/Riddle.java

# run main class with specified class path 
$ java -classpath ./src/ Riddle

Class Path is the directory that contains the .class compiled java files

SaleemKhair
  • 499
  • 3
  • 12