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

时间:2011-09-30 06:05:07

标签: java

我似乎无法使此代码正常工作。这是我不断得到的错误:

  

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

是什么导致这种情况?

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;
}


}

6 个答案:

答案 0 :(得分:9)

类必须为publicmain必须为静态

public static void main(String argv[])

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

答案 1 :(得分:2)

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

答案 2 :(得分:1)

需要更多细节

你遵守了吗?

  

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

您似乎只使用源文件

运行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所说。

答案 3 :(得分:1)

我简化了你的代码以揭示内部的问题。

// Payroll3.java

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

          }
}
}

首先,我们可以看到最可疑的class main隐藏了您需要的类和主要功能。您需要删除class main

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

更改后,您可以获得:

// Payroll3.java

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

    }
}

答案 4 :(得分:0)

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

答案 5 :(得分:0)

编译时应使用该文件的名称,即javac test.java,其中test.java是文件名。并且在运行文件时,类的名称是应该使用的文件,例如,如果它是类Test {}然后是java test