使用 Java 反射动态创建类,java.lang.ClassNotFoundException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2566754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamically class creating by using Java Reflection, java.lang.ClassNotFoundException
提问by rubby
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. Upon reading the name of the class, it will automatically and dynamically (!) generate that class and call its writeoutmethod. If that class is not read from input, it will not be initialized.
我想在java中使用反射,我想做第三个类将从控制台读取类的名称作为字符串。在读取类的名称后,它将自动和动态(!)生成该类并调用其writeout方法。如果该类未从输入中读取,则不会对其进行初始化。
I wrote that codes but I am always taking to "java.lang.ClassNotFoundException", and I don't know how I can fix it.
Can anyone help me?
我写了那些代码,但我总是喜欢“ java.lang.ClassNotFoundException”,我不知道如何修复它。谁能帮我?
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());
}
}
}
回答by akf
Java will throw a ClassNotFoundExceptionwhen you try to reflect on a class name and a class with that name cannot be located in the classpath. 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.Stringinstead of just String)
ClassNotFoundException当您尝试考虑类名并且无法在类路径中找到具有该名称的类时,Java 会抛出。您应该确保您尝试实例化的类在类路径上,并且您使用其完全限定名称(例如:java.lang.String而不是仅仅String)
EDIT: you do not need to use the 3 arg forNamemethod on Class. Instead, use the 1 arg forNamethat takes only the class name that you are passing in.
编辑:您不需要forName在Class. 相反,使用 1 argforName只接受您传入的类名。
回答by Todd R
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. If you run into a class that requires arguments in it's constructor, your call to "cls.newInstance()" will barf.
另外,请注意您的代码仅适用于具有默认(或没有 arg)构造函数的类。如果您遇到一个在其构造函数中需要参数的类,那么您对“cls.newInstance()”的调用将会失败。

