2

The code below compiled successfully here https://www.compilejava.net/ but execution fails

Error: Could not find or load main class ClassDemo

whereas it does have a main entry point. Why ?

package com.tutorialspoint;

import java.lang.reflect.*;

public class ClassDemo {

   public static void main(String[] args) {

     try {            
        ClassDemo c = new ClassDemo();
        Class cls = c.getClass();

        // returns the array of Field objects
        Field[] fields = cls.getDeclaredFields();
        for(int i = 0; i < fields.length; i++) {
           System.out.println("Field = " + fields[i].toString());
        }
     }
     catch(Exception e) {
        System.out.println(e.toString());
     }
   }

   public ClassDemo() {
      // no argument constructor
   }

   public ClassDemo(long l, int i) {
      this.l = l;
      this.i = i;
   }

   long l = 77688;
   int i = 3;
}
user310291
  • 36,946
  • 82
  • 271
  • 487

2 Answers2

4

You need to remove the package identifier from your code (first line) since you are using an online compiler/executor.

Hope this helps.

1

It's because you have a package statement.

Remove that and it will work just fine.

Stultuske
  • 9,296
  • 1
  • 25
  • 37