如何解决“错误:无法找到或加载主类” [英] How to solve"error: could not find or load main class"
问题描述
我是java编程的新手,我编写了一个简单的代码:
package test1;
public class Test1 {
public Test1(){
public static void test( int num1, int num2, int num3){
num1 = 20 ;
num2 = 22 ;
num3 = num1 + num2;
System.out.println(num3);
}
}
}
当我尝试运行它时,我收到错误错误:无法在日食霓虹灯中查找或加载主类。
我尝试过的方法:
我试过运行这个程序,并用cmd打招呼世界程序,我又得到了这个错误。
我已经搜索了很多,但没有一个解决方案有效。
首先,我注意到你正在尝试使用嵌套函数遗憾的是,在Java中,它们在Java语言中完全不受支持。
public Test1(){
public 静态 void test( int num1, int num2, int num3){
您需要终止构造函数,然后启动另一个函数。有一个 lambda函数的概念[ ^ ],但这是别的。
我在代码中看到的另一件事是你没有遵循Java程序中main函数的标准。如果您阅读了 Java的主要功能的标准[ ^ ],你会发现该函数必须声明为,
public static void main( String [] args){}
这是您的程序必须遵循的标准。请尝试使用以下代码:
public class Test1 {
public Test1(){
}
// 添加此
public static void main( String [] args){
// 调用函数
test( 0 , 0 , 0 ); // 因为您稍后会设置值。
}
public static void test( int num1, int num2, int num3){
num1 = 20 ;
num2 = 22 ;
num3 = num1 + num2;
System.out.println(num3);
}
}
这应该有效,除非你的Java编译器期望另一个类成为你程序的主类。其次,从这里开始学习Java, Trail:学习Java语言(The Java™Tutorials) [ ^ ],我非常喜欢从这里学习Java基础知识并申请它们在NetBeans中。 NetBeans是一个适用于Java初学者的简单IDE,有时Eclipse可能很难。
如需更多信息,请查看:
教训:仔细看看Hello World!应用程序(Java™教程>入门) [ ^ ]
I am new to java programming and I wrote a simple code :
package test1;
public class Test1 {
public Test1() {
public static void test (int num1,int num2,int num3) {
num1= 20 ;
num2= 22 ;
num3= num1+num2 ;
System.out.println(num3);
}
}
}
and when I try to run it I get the error "Error: Could not find or load main class" in eclipse neon.
What I have tried:
I tried running this program and also hello world program with cmd and I get this error again.
I have searched alot but none of the solutions worked.
First of all, I noticed that you are trying to use nested functions in Java, sadly, they are not at all supported in the Java language.
public Test1() { public static void test (int num1,int num2,int num3) {
You need to terminate the constructor, and then start another function. There is a concept of lambda functions[^], but that is something else.
Another thing that I saw in your code was that you are not following the standards for main function in Java programs. If you read the standard for Java's main function[^], you will find out that the function has to be declared as,
public static void main(String[] args) { }
This is the standard that your program must follow. Try the following code instead,
public class Test1 { public Test1() { } // Add this public static void main(String[] args) { // Call your function test(0, 0, 0); // Because you are setting values later. } public static void test (int num1,int num2,int num3) { num1= 20 ; num2= 22 ; num3= num1+num2 ; System.out.println(num3); } }
This should work, unless your Java compiler is expecting another class to be the main class for your program. Secondly, start learning Java from here, Trail: Learning the Java Language (The Java™ Tutorials)[^], I really enjoyed learning the Java basics from here and apply them in NetBeans. NetBeans is a simple IDE for Java beginners, Eclipse can be hard sometimes.
For more, please look:
Lesson: A Closer Look at the "Hello World!" Application (The Java™ Tutorials > Getting Started)[^]
这篇关于如何解决“错误:无法找到或加载主类”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!