【问题标题】:could not find or load main class.java找不到或加载主类.java
【发布时间】:2016-03-03 04:28:16
【问题描述】:

我在 JAVA 中有两个用于函数重载的类,即 ABC 和 XYZ。 XYZ 类具有函数重载方法,而 ABC 类具有 main 方法。我已将文件保存为 ABC.java。该程序确实可以编译,但在运行时显示错误“无法找到或加载主类 ABC”。我还附上了具体的代码。

class XYZ
{
   void pqr(int a, int b)
   {
      int res = a*b;
      System.out.println("The result is "+res);
   }
   void pqr(String a, String b)
   {
     System.out.println("The concatenated string is "+a+b);
   }
   void pqr(int a, int b, int c)
{ 
int res = a+b+c;
    System.out.println("The final result is "+res);
}
}

class ABC

{
 public static void main(String[] args)
{
    XYZ a = new XYZ();
    a.pqr(10,20);
    a.pqr("Pratik","Paul");
    a.pqr(20, 40, 60);
 }

}

【问题讨论】:

  • 我认为没有错误
  • 公开课程ABC
  • @Pratik:你能提到这些文件的文件夹路径吗?你还在用 eclipse 还是记事本工作?
  • @Lathy:这些文件的路径是 D:\Pratik\Java。目前我正在使用 notepad++ 并使用 javac 完成编译
  • @sandip:我做了,但出现了另一个错误,上面写着“ABC 类是公共的,应该在名为 ABC.java 的文件中声明”

标签: java main


【解决方案1】:

按照以下步骤,

第一步:

go the bin folder of Java from command prompt(I assume you are trying to run the program from command prompt).

ex: C:\Program Files\Java\jdk1.8.0_05\bin

第二步:

javac <your ABC.java file with full path>

ex: javac C:\Test\ABC.java

similary do for XYZ.java

第三步:

Go to C:\Test\ from command prompt and run,
java ABC

【讨论】:

  • 它应该可以工作。我发布了我所做的。你错过了一些东西。仍然无法加载主类 ABC?
  • 你能把你的代码发给我吗?也许我可以比较一下,找出问题所在。我看到的另一件事是,在同一个类 XYZ 中编写 main 方法时没有错误。仅在使用不同的类时才会出现此问题。
  • 我想你可能没有设置类路径
  • 转到环境变量->系统变量->新建->变量:CLASSPATH。 value : 你的类文件目录并编译 ABC 文件。它应该编译然后运行它
  • 这就是拥有 IDE 的美妙之处。就像你在没有剑的情况下打仗一样。 IDE 将负责设置类路径..
猜你喜欢
  • 2022-06-13
  • 2016-01-16
  • 2021-06-06
  • 1970-01-01
  • 2015-08-04
  • 2016-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多