简体   繁体   English

使用Java Reflection,java.lang.ClassNotFoundException动态创建类

[英]Dynamically class creating by using Java Reflection, java.lang.ClassNotFoundException

I want to use reflection in java, I want to do that third class will read the name of the class as String from console. 我想在java中使用反射,我想做第三个类将从控制台读取类的名称作为String。 Upon reading the name of the class, it will automatically and dynamically (!) generate that class and call its writeout method. 在读取类的名称后,它将自动动态地(!)生成该类并调用其writeout方法。 If that class is not read from input, it will not be initialized. 如果未从输入中读取该类,则不会对其进行初始化。

I wrote that codes but I am always taking to " java.lang.ClassNotFoundException ", and I don't know how I can fix it. 我写了那些代码,但我总是采取“ java.lang.ClassNotFoundException ”,我不知道如何解决它。 Can anyone help me? 谁能帮我?

class class3 {  
   public Object dynamicsinif(String className, String fieldName, String value) throws Exception
   {    
      Class cls = Class.forName(className,true,null);    
      Object obj = cls.newInstance();    
      Field fld = cls.getField(fieldName);    
      fld.set(obj, value);    
      return obj;    
  }

  public void writeout3()    
  {    
      System.out.println("class3");    
  }    
}

public class Main {        
    public static void main(String[] args) throws Exception    
    {            
           System.out.println("enter the class name : ");    
       BufferedReader reader= new BufferedReader(new InputStreamReader(System.in));
           String line=reader.readLine();    
           String x="Text1";    
           try{    
              class3 trycls=new class3();    
              Object gelen=trycls.dynamicsinif(line, x, "rubby");    
              Class yeni=(Class)gelen;        
              System.out.println(yeni);                    
          }catch(ClassNotFoundException ex){        
              System.out.print(ex.toString());    
          }    
    }    
}

Java will throw a ClassNotFoundException when you try to reflect on a class name and a class with that name cannot be located in the classpath. 当您尝试反映类名时,Java将抛出ClassNotFoundException ,并且具有该名称的类不能位于类路径中。 You should ensure that the class you are trying to instantiate is on the classpath and that you use its fully qualified name (ex: java.lang.String instead of just String ) 您应该确保您尝试实例化的类在类路径上,并且您使用其完全限定名称(例如: java.lang.String而不是String

EDIT: you do not need to use the 3 arg forName method on Class . 编辑:您不需要在Class上使用3 arg forName方法。 Instead, use the 1 arg forName that takes only the class name that you are passing in. 而是使用1 arg forName ,它只接受您传入的类名。

A common mistake when trying to instantiate object through reflection is to pass just the class name, not the fully qualified name. 尝试通过反射实例化对象时常见的错误是只传递类名,而不传递完全限定名。 In other words, using "String" instead of "java.lang.String" will not work. 换句话说,使用“String”而不是“java.lang.String”将不起作用。

Also, be aware that your code will only work for classes that have a default (or no arg) constructor. 另外,请注意您的代码仅适用于具有默认(或无arg)构造函数的类。 If you run into a class that requires arguments in it's constructor, your call to "cls.newInstance()" will barf. 如果遇到需要在其构造函数中创建参数的类,则对“cls.newInstance()”的调用将为barf。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM