0

newbie here. Tried executing Java class that has a nested class and I keep getting this Error: Could not find or load main class . Caused by: java.lang.ClassNotFoundException When I compiled the source code, I got two new .class files; ShadowTest$FirstLevel.class and ShadowTest.class. The error shows up when I try to execute either one. Please help.

Here's the code

public class ShadowTest {

    public int x = 0;

    class FirstLevel {

        public int x = 1;

        void methodInFirstLevel(int x) {
            System.out.println("x = " + x);
            System.out.println("this.x = " + this.x);
            System.out.println("ShadowTest.this.x = " + ShadowTest.this.x);
        }
    }

    public static void main(String... args) {
        ShadowTest st = new ShadowTest();
        ShadowTest.FirstLevel fl = st.new FirstLevel();
        fl.methodInFirstLevel(23);
    }
}
knittl
  • 246,190
  • 53
  • 318
  • 364
Ten Kei
  • 3
  • 2

3 Answers3

1

I tried to replicate the issue you are facing by following the below steps.

  • I have created a java file "ShadowTest.java" with your code.
public class ShadowTest {

    public int x = 0;

    class FirstLevel {

        public int x = 1;

        void methodInFirstLevel(int x) {
            System.out.println("x = " + x);
            System.out.println("this.x = " + this.x);
            System.out.println("ShadowTest.this.x = " + ShadowTest.this.x);
        }
    }

    public static void main(String... args) {
        ShadowTest st = new ShadowTest();
        ShadowTest.FirstLevel fl = st.new FirstLevel();
        fl.methodInFirstLevel(23);
    }
}
  • Compiled the ShadowTest.java using javac ShadowTest.java, two class files were created


    D:\test>javac ShadowTest.java

    D:\test>dir
    Volume in drive D is MonWork
    Volume Serial Number is 3A72-52A4

    Directory of D:\test

    17-07-2021 13:01 .
    17-07-2021 13:01 ..
    17-07-2021 13:01 901 ShadowTest$FirstLevel.class
    17-07-2021 13:01 529 ShadowTest.class
    17-07-2021 13:01 539 ShadowTest.java
    3 File(s) 1,969 bytes
    2 Dir(s) 55,102,857,216 bytes free

output of javac command and newly created class files in the same directory

  • On executing ShadowTest class, with java ShadowTest command. I am getting the expected output.
    OUTPUT:
    D:\test>java ShadowTest
    x = 23
    this.x = 1
    ShadowTest.this.x = 0

    output of java command and its output

  • To reproduce your issue I tried to delete ShadowTest$FirstLevel.class file and execute java ShadowTest. But did not get the exact error you are getting.

Error: Could not find or load main class . Caused by: java.lang.ClassNotFoundException

*D:\test>del ShadowTest$FirstLevel.class

D:\test>java ShadowTest
Exception in thread "main" java.lang.NoClassDefFoundError: ShadowTest$FirstLevel
        at ShadowTest.main(ShadowTest.java:18)
Caused by: java.lang.ClassNotFoundException: ShadowTest$FirstLevel
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more*

executing main class after deleting the class file for the inner class.

Please share the complete error message and commands you are using for compiling and executing the class.

  • It was just a scenario to reproduce the issue. According to the post the root cause was Caused by: java.lang.ClassNotFoundException. So deleting the class file after compilation was a way to get the exception. – Shashi Shankar Jul 17 '21 at 08:26
  • I just ran it the way you and it worked. I realised my mistake, the previous tutorial I did used assert statements, so instead of using -cp at the command line, I kept using -ea. My bad for the inconvenience, but learnt a lot thought, thank you. – Ten Kei Jul 17 '21 at 08:46
0

You can always compile the class with javac filename command. Then execute the code with command java main_class_name

javac ShadowTest.java
java ShadowTest

/** output **/

x = 23
this.x = 1
ShadowTest.this.x = 0

Inner classes, if any present in your class, will be compiled and the class file will be ClassName$InnerClassName.

Uday Chauhan
  • 1,071
  • 1
  • 9
  • 19
0

use this online compiler ,it's executing your code error free

your code:

enter image description here

output :

enter image description here

saqib kafeel
  • 296
  • 3
  • 8