错误:无法找到或加载主类<<<为什么我会收到此错误?

发布于 2024-12-07 02:20:22 字数 3020 浏览 0 评论 0 原文

我似乎无法让这段代码正常工作。这是我不断收到的错误:

错误:无法找到或加载主类。

是什么原因造成的?

Payroll3.java

// A program to calculate and print the department, name, and pay of an employee.
import java.util.Scanner; //program uses the Scanner class
import java.io.*;

class main
{
public class Payroll3

{
// main method begins execution of Java program.
    public void main(String args[])
          {
   // create Scanner to obtain input from the command window
   Scanner input = new Scanner (System.in);

   double number1; // first number to multiply
   double number2; // second number to multiply
   double product; // product of number1 and number2


while(true){   // infinite loop


   System.out.print("Enter Department name: "); //prompt
   String name = input.nextLine(); // read name from user

    if(name.equals("stop"))  // exit the loop
        break;


   System.out.print("Enter number of employees: "); // prompt
   number1 = input.nextDouble(); // read first number from user
   input.nextLine();
      while( number1 <= -1){
         System.out.print("Enter positive number of employees:"); // prompt
         number1 = input.nextDouble(); // read first number from user
         input.nextLine();
      } /* while statement with the condition that negative numbers are entered
         user is prompted to enter a positive number */

   System.out.print("Enter average salary: "); // prompt
   number2 = input.nextDouble(); // read second number from user
   input.nextLine();
      while( number2 <= -1){
       System.out.print("Enter positive number for average salary:"); // prompt
        number2 = input.nextDouble(); // read first number from user
        input.nextLine();                    
 } /* while statement with the condition that negative numbers are entered
         user is prompted to enter a positive number */

   // make department object
        Department dept = new Department(name,number1,number2);

   product = number1 * number2; // multiply numbers

   System.out.println("Department name:" + name); // display Department name
   System.out.printf("Payroll is: $%.2f\n", product); // display product

   } // end while method

} // end method main

}/* end class Payroll3 */
}

//  Stores data about an department
class Department {

//fields
String name;
double number1;
double number2;

// constructor
public Department(String name, double number1, double number2) {
    this.name = name;
    this.number1 = number1;
    this.number2 = number2;
}

// returns the pay:
public double getPay() {
    return number1*number2;
}

// getters and setters

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public double getnumber1() {
    return number1;
}

public void setNumber1(double number1) {
    this.number1 = number1;
}

public double getNumber2() {
    return number2;
}

public void setNumber2(double number2) {
    this.number2 = number2;
}


}

I can't seem to get this code to work correctly. This is the error I keep getting:

Error: Could not find or load main class.

What causes this?

Payroll3.java

// A program to calculate and print the department, name, and pay of an employee.
import java.util.Scanner; //program uses the Scanner class
import java.io.*;

class main
{
public class Payroll3

{
// main method begins execution of Java program.
    public void main(String args[])
          {
   // create Scanner to obtain input from the command window
   Scanner input = new Scanner (System.in);

   double number1; // first number to multiply
   double number2; // second number to multiply
   double product; // product of number1 and number2


while(true){   // infinite loop


   System.out.print("Enter Department name: "); //prompt
   String name = input.nextLine(); // read name from user

    if(name.equals("stop"))  // exit the loop
        break;


   System.out.print("Enter number of employees: "); // prompt
   number1 = input.nextDouble(); // read first number from user
   input.nextLine();
      while( number1 <= -1){
         System.out.print("Enter positive number of employees:"); // prompt
         number1 = input.nextDouble(); // read first number from user
         input.nextLine();
      } /* while statement with the condition that negative numbers are entered
         user is prompted to enter a positive number */

   System.out.print("Enter average salary: "); // prompt
   number2 = input.nextDouble(); // read second number from user
   input.nextLine();
      while( number2 <= -1){
       System.out.print("Enter positive number for average salary:"); // prompt
        number2 = input.nextDouble(); // read first number from user
        input.nextLine();                    
 } /* while statement with the condition that negative numbers are entered
         user is prompted to enter a positive number */

   // make department object
        Department dept = new Department(name,number1,number2);

   product = number1 * number2; // multiply numbers

   System.out.println("Department name:" + name); // display Department name
   System.out.printf("Payroll is: $%.2f\n", product); // display product

   } // end while method

} // end method main

}/* end class Payroll3 */
}

//  Stores data about an department
class Department {

//fields
String name;
double number1;
double number2;

// constructor
public Department(String name, double number1, double number2) {
    this.name = name;
    this.number1 = number1;
    this.number2 = number2;
}

// returns the pay:
public double getPay() {
    return number1*number2;
}

// getters and setters

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public double getnumber1() {
    return number1;
}

public void setNumber1(double number1) {
    this.number1 = number1;
}

public double getNumber2() {
    return number2;
}

public void setNumber2(double number2) {
    this.number2 = number2;
}


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

树深时见影 2024-12-14 02:20:22

类必须是 public 且 main 必须是 static

public static void main(String argv[])

并且不能有嵌套的主类。至少这可能不是你所期望的。为什么不从简单的教程开始并用代码扩展它呢?

Class must be public and main must be static

public static void main(String argv[])

And you can't have nested main class. At least it's probably not what you expect. Why don't you start with simple tutorial and expand it with your code?

走过海棠暮 2024-12-14 02:20:22

运行程序时只需输入 java program_nameNOT java program_name.java

while Running the program just type java program_name but NOT java program_name.java

聽兲甴掵 2024-12-14 02:20:22

需要更多详细信息

您遵守了吗?

错误:无法找到或加载主类。?

看来你只是用源文件运行java

java test.java # wrong
Exception in thread "main" java.lang.NoClassDefFoundError: test/java
Caused by: java.lang.ClassNotFoundException: test.java
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: test.java. Program will exit.

# correct
javac yourfile.java
java yourfile

另外你还需要像Alex Gitelman 所说的那样更改你的文件。

more detail is required

did you complied it?

Error: Could not find or load main class.?

it seems that you just run the java with the source file

java test.java # wrong
Exception in thread "main" java.lang.NoClassDefFoundError: test/java
Caused by: java.lang.ClassNotFoundException: test.java
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: test.java. Program will exit.

# correct
javac yourfile.java
java yourfile

Also you need to changed your file as Alex Gitelman told.

洒一地阳光 2024-12-14 02:20:22

我简化了你的代码以揭示里面的问题。

// Payroll3.java

class main
{
public class Payroll3
{
    public void main(String args[])
          {

          }
}
}

首先,我们可以看到最外面的可疑的class main隐藏了你需要的类和main函数。您需要删除class main

其次,主函数需要声明为类函数而不是实例函数,因为它不绑定到任何实例。在java中,我们使用static来声明类变量/类函数。所以需要将 main 声明为 public static void main(String argv[])

修改后,可以得到:

// Payroll3.java

public class Payroll3
{
    public static void main(String argv[])
    {

    }
}

I simplified your code to reveal the problem inside.

// Payroll3.java

class main
{
public class Payroll3
{
    public void main(String args[])
          {

          }
}
}

First, we can see that the outmost suspicious class main hide your needed class and the main function. You need to remove the class main.

Second, main function need to be declared as class-function instead of instance-function, because it is not bound to any instance. In java, we use static to declare a class-variable/class-function. So you need to declare the main as public static void main(String argv[])

After the changes, you can get:

// Payroll3.java

public class Payroll3
{
    public static void main(String argv[])
    {

    }
}
鱼忆七猫命九 2024-12-14 02:20:22

问题是 main 函数需要声明为 public static void main(String[] args),而不是 public void main(String[] args)

The problem is that the main function needs to be declared as public static void main(String[] args), not public void main(String[] args).

浅语花开 2024-12-14 02:20:22

编译时应使用文件名,即 javac test.java,其中 test.java 是文件名。当运行文件时,类的名称是应该使用的文件,例如,如果它是 class Test{} 那么 java test

the name of the file should be used when compiling it i.e. javac test.java where test.java is the file name. And when running the file the name of the class is the file should be used for example if it is class Test{} then java test

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文