英文:
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 <= 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 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 <= 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.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论