Exception in thread “main” java.lang.NoClassDefFoundError: Solution (wrong name: solution)

huangapple go评论72阅读模式
英文:

Exception in thread "main" java.lang.NoClassDefFoundError: Solution (wrong name: solution)

问题

import java.util.*;
class solution
{
    void table(final int n) {
        for (int i = 1; i <= 10; i++) {
            System.out.println(n + " X " + i + " = " + n * i);
        }
    }
}

class Solution {
    public static void main(final String args[]) {
        solution os = new solution();
        Scanner ob = new Scanner(System.in);
        int n = ob.nextInt();
        os.table(n);
    }
}

After running java Solution, this is the error message:

Exception in thread "main" java.lang.NoClassDefFoundError: Solution (wrong name: solution)
        at java.base/java.lang.ClassLoader.defineClass1(Native Method)
        at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
        at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
        at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:821)
        at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:719)
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:642)
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:600)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        at Solution.main(table.java:13)

Could not find the solution for it. This error starts popping when I have upgraded to Java 14; earlier I had Java 8, and this code runs with no problem.

英文:
class solution
{
    void table(final int n) {
        for (int i = 1; i &lt;= 10; i++) {
            System.out.println(n + &quot; X &quot; + i + &quot; = &quot; + n * i);
        }
    }
}

class Solution {
    public static void main(final String args[]) {
        solution os = new solution();
        Scanner ob = new Scanner(System.in);
        int n = ob.nextInt();
        os.table(n);
    }
}

After running java Solution
this is the error message

Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: Solution (wrong name: solution)
        at java.base/java.lang.ClassLoader.defineClass1(Native Method)
        at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
        at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
        at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:821)
        at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:719)
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:642)
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:600)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        at Solution.main(table.java:13)

could not find the solution for it this error starts popping when I have upgraded to Java 14 earlier I have java 8 this code runs with no problem.

答案1

得分: 0

始终遵循最佳实践。不要在同一个包中的类中使用相同的名称。

package com.india.demo;

public class solution
{
    public void table(final int n) {
        for (int i = 1; i <= 10; i++) {
            System.out.println(n + " X " + i + " = " + n * i);
        }
    }
}
------------------
package com.india.string;

import java.util.Scanner;

import com.india.demo.solution;

public class Solution {
    public static void main(final String args[]) {
        solution os = new solution();
        Scanner ob = new Scanner(System.in);
        int n = ob.nextInt();
        os.table(n);
    }
}

This will work.
英文:

Always follow best practices. Don't use the same name for class in the same package.

package com.india.demo;

public class solution
{
    public void table(final int n) {
        for (int i = 1; i &lt;= 10; i++) {
            System.out.println(n + &quot; X &quot; + i + &quot; = &quot; + n * i);
        }
    }
}
------------------
package com.india.string;

import java.util.Scanner;

import com.india.demo.solution;

public class Solution {
    public static void main(final String args[]) {
        solution os = new solution();
        Scanner ob = new Scanner(System.in);
        int n = ob.nextInt();
        os.table(n);
    }
}

This will work.

</details>



huangapple
  • 本文由 发表于 2020年8月29日 16:20:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63644902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定