Java 使用类时未找到类异常

Java 使用类时未找到类异常,java,classnotfound,Java,Classnotfound,请检查以下代码。。。。类testError已实例化,但仍生成“未找到类”异常。。。若这是真的,那个么为什么在异常处理程序中编写的语句不会被打印呢 class testError { void display() { System.out.println("This is testError Class"); } } class checkResult { public static void main(String[] args) {

请检查以下代码。。。。类testError已实例化,但仍生成“未找到类”异常。。。若这是真的,那个么为什么在异常处理程序中编写的语句不会被打印呢

class testError
{
  void display()
    {
      System.out.println("This is testError Class");
    }

}

class checkResult
{
  public static void main(String[] args)
     {
        testError te = new testError();
        te.display();// I hope the class has been created

        Class cls = Class.forName("testError"); // will throw ClassNotFound exception
                                                // Why??... Though the class has been 
                                                // instantiated

        // if we try to put it in trycatch block it will work...Why??

         try{ Class cls = Class.forName("testError");}

         catch(ClassNotFoundException e)
            {
              System.out.println("Error found");  //"Error found" will not be printed
                                                  // as the class has been instantiated
            }

      }
   }

我不能发表评论-因为我的声誉太低,但您的代码运行和调试都很好-尽管我不得不对其进行一些修改以使其可编译:

    public static void main(String[] args) throws ClassNotFoundException {
    testError te = new testError();

    Class<?> cls = Class.forName("testError"); 
    try {
        cls = Class.forName("testError");
        // If you got there then everything went fine
        te.display();
    }
    catch (ClassNotFoundException e) {
        System.out.println("Error found"); 
    }
}
publicstaticvoidmain(字符串[]args)抛出ClassNotFoundException{
testError te=新的testError();
Class cls=Class.forName(“testError”);
试一试{
cls=Class.forName(“测试错误”);
//如果你到了那里,一切都会好起来的
te.display();
}
catch(classnotfounde异常){
System.out.println(“发现错误”);
}
}
如何运行代码(在IDE中从命令行)?控制台输出是什么? 如果你想让人们调查你的问题,你必须提供更多有用的信息


最后,Java约定指定类名称应以大写字符开头(CheckResultTestError)。另外,您应该避免在默认包中使用类,因为这些类无法导入。

首先遵循java命名约定

公开你的主课

创建一些像mypackage这样的包(不适合在默认包中创建),并将类放在其中

并尝试以这种方式调用该方法

String name = packageName.className.class.getName();//get the name of the class
className o = (className)Class.forName(name)
                               .newInstance();
//will give an instance of type Object so cast it
o.display();// call the method

我猜这是Java?您需要传递完全限定的类名。请参阅文档:这些类是真的在默认包中,还是在您定义的某个包中?嗨。。这是在默认包中..问题是类已实例化,因此将在类路径中,但如果尝试执行代码,则会抛出类未找到异常..hii RPARRE..如果完整的类名是原因,则错误也会出现在try catch块中..但它的工作机制是第一个
Class.forName(“testError”)真的抛出CNFEException吗?或者Java只是在抱怨您应该捕获这个异常——因为在此之前您不会捕获或抛出任何异常?对于第二种情况,您可能应该阅读,然后看看hii。。。您的代码将运行良好,因为您已经添加了抛出ClassNotFoundException…我的问题是-->Class cls=Class.forName(“testError”);正在抛出ClassNotFound异常…如果ClassNotFound事实为true,则必须在控制台上打印try catch块中的print语句…您的代码将正常工作,因为您已经添加了抛出ClassNotFound异常…我的问题是---->Class cls=Class.forName(“testError”);正在引发ClassNotFound异常…如果ClassNotFound事实为true,则必须在控制台上打印try-catch块中的print语句…按…进行操作。首先尝试注释try-catch块-->ClassNotFound Exception将被丢弃…现在注释//Class cls=Class.forName(“testError”)并取消对try-catch块的注释..这一次它可以正常工作..为什么???。如果存在ClassNotFound异常,那么控件必须进入try-catch块,并且应该打印“Error-find”,因为(我)预期注释try/catch块不会改变任何内容,我仍然会执行罚款。我在程序结束时调用了
te.display()
,得到了
这是testError类
,没有引发异常。。。