简体   繁体   中英

How can I fix the error “java.lang.NoClassDefFoundError” that has occured after moving my java files in packages?

Okay, so I'm learning Java, and have just moved all of my test files into different folders, with package headers. Yet the compiler states the error java.lang.NoClassDefFoundError.

The class file is clearly there, and yet it cannot find it?

I'm using VS Code is that's any help. The files work in Intellij, but I'm trying to get to know VS Code as it's a good 'all around' editor.

I've looked all over but the symptoms of what others have experienced are different to mine.

package loops;

public class EnhancedForLoop {
public static void main(String []args) {
    int [] numbers = {5, 10, 15, 20, 25};
    for(int x : numbers ) {
        System.out.println( x + "," );
    }

    System.out.println("\n");


    String [] names = {"Kettle", "Pot", "Cup", "Jug"};
    for(String name : names) {
        System.out.println(name + ",");
    }
}
}

Like I said, this should run fine. But after moving the files it doesn't :(.

The full error message is:

[Running] cd "c:\Users\nickr\Documents\VS Workspace\Project 1\src\loops\" && javac EnhancedForLoop.java && java EnhancedForLoop
Error: Could not find or load main class EnhancedForLoop
Caused by: java.lang.NoClassDefFoundError: loops/EnhancedForLoop (wrong name: EnhancedForLoop)

Maybe you can use -verbose to see the detail.

cd "c:\Users\nickr\Documents\VS Workspace\Project 1\src\loops\" && javac EnhancedForLoop.java && java -verbose EnhancedForLoop

I think java could not find the EnhancedForLoop in classpath because of you didn't set the classpath.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM