【问题标题】:Java file Error: Could not find or load main classJava 文件错误:无法找到或加载主类
【发布时间】:2017-01-17 07:25:04
【问题描述】:

我在将程序加载到 linux 服务器时遇到问题。我能够从我正在使用的 IDE 中复制粘贴我的代码,并让服务器编译并运行代码,但执行为空。我将行 println 更改为 printf 并且文件将无法编译并且每次都给我错误“无法找到或加载主类 Program01”。包括当我粘贴刚刚工作的旧代码时,我正在尝试的一切都失败了。寻找关于我可以解决的其他意见。它从编号为“312032486”的输入文件中提取。只是想知道为什么它无法找到或加载主类。我相信其他一切都按预期工作。

感谢所有查看并打开任何设备的人,因为我是 java 新手。

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package program01;

import java.util.Scanner;

/**
*
* @author Devin
*/
public class Program01 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    // Create variables for birth rate, death rate and immigration raet
    int birthRate = 1 / 7;
    int deathRate = 1 / 13;
    int immigrationRate = 1 / 45;

    // Variable creating the number of seconds per year
    int secondsPerYear = 60 * 60 * 24 * 365;

    // Variable to find births, deaths and immigrants added per year
    int birthsPerYear = birthRate * secondsPerYear;
    int deathsPerYear = deathRate * secondsPerYear;
    int immigrantsPerYear = immigrationRate * secondsPerYear;

    // Scanner method to GET population through input.data
    Scanner sc = new Scanner (System.in);
    System.out.printf("Enter population: ");
    int population = sc.nextInt();
    System.out.printf("Population is: " + population);

    // Math to create variables for population per X year
    double populationYear0 = population;
    double populationYear1 = populationYear0 + birthsPerYear - deathsPerYear + immigrantsPerYear;
    double populationYear2 = populationYear1 + birthsPerYear - deathsPerYear + immigrantsPerYear;
    double populationYear3 = populationYear2 + birthsPerYear - deathsPerYear + immigrantsPerYear;
    double populationYear4 = populationYear3 + birthsPerYear - deathsPerYear + immigrantsPerYear;
    double populationYear5 = populationYear4 + birthsPerYear - deathsPerYear + immigrantsPerYear;

    // Print out the variables data from code above
    System.out.println("Population Year 0: " + populationYear0);
    System.out.println("Population Year 1: " + populationYear1);
    System.out.println("Population Year 2: " + populationYear2);
    System.out.println("Population Year 3: " + populationYear3);
    System.out.println("Population Year 4: " + populationYear4);
    System.out.println("Population Year 5: " + populationYear5);

}

}

【问题讨论】:

  • 你执行的命令是什么?
  • 使用我学校的 linux 服务器,使用名为 SubmitJ 的命令。然后它让我选择主类,当我选择这个程序时,Program01 它给了我错误。我的其他 2 个主课程序运行良好。
  • 他们也有包吗?你编译成jar 还是classes?服务器是指在线法官吗?
  • @Devin 你能附加另一个你成功部署在服务器上的应用程序吗?
  • 这是我通过 PUTTY 使用 linux 服务器看到的链接。 HelloWorld.Java 和 JavaApplication1.java 都可以完美执行,但 Program01 没有。 gyazo.com/9ca6402b71ee70377a813bad0c899b3b

标签: java


【解决方案1】:

在您在 cmets 中发表评论后,我确信删除 package program01; 就足够了。如果你的类在某个包中,你必须将文件放在相应的目录中。

OP文件夹结构为:

/cs116
    Program01.class

所以包声明无效。删除包声明将解决问题。

【讨论】:

  • 在我自己的帖子下用屏幕截图回复,其他 2 个类/java 文件可以正常工作,但 Program01 不能。
  • java program01.Program01.class 错误,你应该NOT 指定类扩展。
  • 这太愚蠢了。将类移动到不同的包以使其工作......如何修复它而不是移动它?
  • @rkosegi 问题出在在线评委,似乎不支持打包。
  • 这与包装无关。您可以随时运行它而无需将类移动到其他包。
猜你喜欢
  • 2017-07-31
  • 1970-01-01
  • 2015-02-25
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 1970-01-01
相关资源
最近更新 更多