【问题标题】:Error Could not find or load main class testing (Java)错误无法找到或加载主类测试(Java)
【发布时间】:2016-02-11 06:27:11
【问题描述】:

// 错误:无法找到或加载主类
我创建了名为 Foo.java 和 Bar.java 的文件,这两个文件都在不同的包中。他们编译成功但我无法执行程序

package demo;
public class Foo
{
   protected int result=20;
   int other=25;
}

第二个文件:Bar.java

package testing;
import demo.Foo;
public class Bar extends Foo
{
 private int sum=10;
   public void Info()
    {
   sum+=result;
 System.out.println(sum);
    }
 public static void main(String[] args)
   {
     Bar b=new Bar();
     b.Info();
   }

}

为了编译,我使用了以下命令:

  javac -d . Foo.java
  javac -d . Bar.java
  cd testing
  java Bar
  Error Could not find or load main class testing (Java)

This is the error i am getting

错误无法找到或加载主类测试(Java)

【问题讨论】:

  • 你为什么要改变你的目录?跑吧。

标签: java


【解决方案1】:

我想你应该使用javac -cp . Foo.java。 (酒吧也一样) 然后你应该用java Foo开始你的程序

【讨论】:

    【解决方案2】:

    有几样东西你可以看看。

    1. 您的类 Foo 和 Bar 是否在系统类路径中。
    2. 您的类 Foo 和 Bar 是否作为层次结构位于包文件夹下。

    例如 Foo 应该在文件夹 demo 下,Bar 应该在文件夹 testing 下。

    干杯,

    苏拉夫

    【讨论】:

      【解决方案3】:

      你只需要做:

      javac -d . Foo.java
      javac -d . Bar.java
      java testing.Bar 
      

      【讨论】:

        【解决方案4】:

        类加载器没有找到 Foo 类,因此它无法加载 Bar(这取决于 Foo)

        在你的情况下,我会按照 Martín Muñoz del Río 在他的回答中所说的那样运行它

        你只需要做:

        javac -d 。 Foo.java

        javac -d 。 Bar.java

        java testing.Bar

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-07-31
          • 1970-01-01
          • 2015-02-25
          • 2023-03-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多