Java类加载器

Java类加载器,java,classloader,Java,Classloader,我需要从目录中的java文件中获取类的方法名 File file=new File("C:/class/"); try { // Convert File to a URL URL url = file.toURL(); // file:/c:/class/ URL[] urls = new URL[]{url}; // Create a new class loader with the directory

我需要从目录中的java文件中获取类的方法名

   File file=new File("C:/class/");
    try {
      // Convert File to a URL
      URL url = file.toURL();          // file:/c:/class/
      URL[] urls = new URL[]{url};

      // Create a new class loader with the directory
      URLClassLoader loader = new URLClassLoader(urls);

      // Load in the class; Class.childclass should be located in
      // the directory file:/c:/class/
      Class cls = loader.loadClass("Arithmatic.java");
      Method[] methods=cls.getMethods();
      for(Method method:methods){
       System.out.println("Method name:"+method.getName());
      }
  } catch (MalformedURLException e) {
   System.out.println("Exception");
  } catch (ClassNotFoundException e) {
   System.out.println("Class not found exception");
  }
我得到
ClassNotFoundException

这是正确的做法吗

任何人都能提出解决方案吗…

试试看

Class.forName("Arithmatic", true, loader)
而不是

loader.loadClass("Arithmatic.java")
试一试

而不是

loader.loadClass("Arithmatic.java")

不能将
.java
文件作为类加载。您应该加载一个
.class
文件(这意味着应该先编译它)


如果您没有编译过的类,您可以在运行时使用编译它。您不能将
.java
文件作为类加载。您应该加载一个
.class
文件(这意味着应该先编译它)

如果您没有编译形式的类,可以在运行时使用