【问题标题】:Class not found exception找不到类异常
【发布时间】:2016-01-15 16:04:57
【问题描述】:

我正在学习 Java 中的反射。我开始知道我们可以很容易地提取带有反射的类文件中使用的方法。

我有一个包含 n 个方法的 java 类文件。

测试文件是指我要从中读取所用方法的 java 类,而 testSample 是我从中访问 Testfile 类方法的类

 public class testSample
    {

        public static void main(String args[]) throws ClassNotFoundException

        {

            File filename = new File(
                    "C:/ProgramData/Eclipse/Projects_3.7.1/Testfile.java");

            String filen = filename.toString();

            if (filen.endsWith(".java"))

            {

                String actfilename = StringUtils.substringBefore(filen, ".java");

                Class classname = Class.forName(actfilename);

                Method[] method = classname.getMethods();

                for (Method methods : method)

                {

                    System.out.println("Method name \r\n" + methods.getName());

                    Class[] parameters = methods.getParameterTypes();

                    for (Class parametername : parameters)

                    {

                        System.out.println("parameters name \r\n"
                                + parametername.getName());

                    }

                }

            }

        }
    }

请帮我解决这个问题。

我得到的异常错误如下

Exception in thread "main" java.lang.ClassNotFoundException: testFile
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at com.TestSample1.retrievemethods(TestSample1.java:58)
    at com.TestSample1.filetraverse(TestSample1.java:43)
    at com.TestSample1.filetraverse(TestSample1.java:45)
    at com.TestSample1.main(TestSample1.java:22)

测试文件

非常感谢您的宝贵建议。但我还没有摆脱它。

让我说清楚。

static File appname="C:/programdata/eclipse/appname";

public static void main(String[] args) throws ClassNotFoundException 

{

        TestSample1 tf1=new TestSample1();

        tf1.filetraverse(appname);

}

public void filetraverse(File appname) throws ClassNotFoundException

{

        if (appname.isDirectory()) 

        {

            File[] files = appname.listFiles();

            for (File filename : files) 

            {

                    String finame=filename.getName().toLowerCase();

                    if(finame.endsWith("java"))

                    {

                            retrievemethods(filename,reflection);

                    }           

                            filetraverse(filename);     

            }           

        }

    }

public void retrievemethods(File filename,File reflection) throws 
ClassNotFoundException

    {

        String classna=filename.toString();

        String classnam=StringUtils.substringBetween(classna, "/appname/", ".java");

        Class reflectclass = Class.forName(classnam);

        Method[] method=reflectclass.getMethods();

        for (Method methods : method)

        {

            System.out.println("Method name \r\n"+methods.getName());

        Class[] parameters=methods.getParameterTypes();

        for (Class parametername : parameters) 

        {

            System.out.println("parameters name \r\n"+parametername.getName());

        }

    }

    }

我得到的错误是找不到类异常。

【问题讨论】:

  • actfilename的内容是什么
  • “我知道我们可以很容易地提取类文件中使用的方法”foo.java 不是类文件,foo.class 是。您还需要在类路径中找到要使用的类包的位置,或者使用 ClassLoader 加载它。

标签: java reflection classnotfoundexception


【解决方案1】:

除非您在 Classpath 上编译了类,否则您的程序将无法运行。

Class.forName(String) 的参数也是期望类路径上的类的完全限定名称,而不是文件路径

【讨论】:

  • 我的包中有两个类文件。一个是testfile,另一个是testsample,Class classname=TestSample2.class;如果 TestSample2 在同一个包中,这将适用于这种情况。 Bot 如果它在不同的包中,我如何检索类名。?我们如何传递文件的整个路径来获取相应的类名? "C:/ProgramData/Eclipse/Projects_3.7.1/TestSample2.java"
  • 请描述您的目录结构。
【解决方案2】:
Class classname = Class.forName(actfilename);

这里的 'actfilename' 应该是完全限定的类名。

例如: 必需的类名 - com.organisation.reflection.Testfile 你的班级名称 - Testfile

当您调用 Class.forName("sampleClass") 时,JVM 会在运行时动态加载该类并返回与“sampleClass”关联的 Class 对象。

例如:

package com.organisation.reflection;

class TestFile {

static {

    System.out.println("TestFile class loaded.");
}

}

package com.organisation.reflection;

public class TestSample {

public static void main(String[] args) {
    try { 
        Class c   = Class.forName("com.organisation.reflection.TestFile"); 
    } catch (ClassNotFoundException e) {
    }

}

}

输出:

TestFile 类已加载。

Check Class.forName here

【讨论】:

  • 在 Eclipse 中工作时,我们在哪里可以找到 .java 的 .class 文件?
  • 问题是它适用于单个类。如果我在一个文件夹中有 n 个类文件,则无法全部读取它们
  • 转到您的项目文件夹并在其中转到您的目标文件夹。在那里你会找到 jar/war 文件和一个名为“Classes”的文件夹。转到“类”文件夹。在那里,您将在包层次结构中找到所有类。
  • 单个文件夹中有多少类文件并不重要。例如:Sample1.java、Sample2.java 和 Sample3.java 存在于同一个包中,即 com.organisation.reflection。为了加载所有上述三个类,您必须使用所有三个类的完全限定名称调用 Class.forName() 三次。 Class.forName("com.organisation.reflection.Sample1") Class.forName("com.organisation.reflection.Sample2") Class.forName("com.organisation.reflection.Sample3")
  • 方法会像这样 public void retrievemethods(File filename,File reflection) throws ClassNotFoundException { Class reflectclass= Class.forName(filename);方法[] 方法=reflectclass.getMethods(); for (Method methods : method) { System.out.println("方法名 \r\n"+methods.getName());类[] 参数=methods.getParameterTypes(); for (类参数名: 参数) { System.out.println("参数名"+parametername.getName()); } } 错误发生在 Class reflectclass= Class.forName(filename);因为我们只能传递字符串作为参数而不是文件
【解决方案3】:

Hurrayy...终于明白了.. :) :) :D :D 非常感谢大家... :) :)

public void retrievemethods(File filename,File reflection) throws ClassNotFoundException  
    {
        Class c=filename.getClass();
        Method[] method=c.getMethods();
        for (Method methods : method) 
        {
            System.out.println("new methods"+methods);
        }
    }

【讨论】:

  • 但是没用.. :( 猜猜输出是什么.. :( 它只是显示文件操作方法,如getabsolutepath,get canonicalpath等... :( :(
猜你喜欢
  • 2020-06-13
  • 2016-06-19
  • 2010-12-03
  • 2013-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多