gpt4 book ai didi

java - 线程 “main”中的异常java.lang.NoClassDefFoundError : Solution (wrong name: solution)

转载 作者:行者123 更新时间:2023-12-02 11:12:53 27 4
gpt4 key购买 nike

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);
}
}
运行java解决方案后
这是错误消息
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)
找不到解决方案,当我较早升级到Java 14时,此错误开始 pop ,我有Java 8,此代码运行正常。

最佳答案

始终遵循最佳做法。不要在同一包中为类使用相同的名称。

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.

关于java - 线程 “main”中的异常java.lang.NoClassDefFoundError : Solution (wrong name: solution),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63644902/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com