在运行时在java中创建类的实例
在我的程序中,我动态生成类,但是当我尝试时:
String[] args = {"-d","D:\\path\\build\\classes","-s","D:\\path\\src","http://services.aonaware.com/DictService/DictService.asmx?WSDL"};
WsImport.doMain(args);
URL url = new URL("file:D:/path/build/classes/com/aonaware/services/webservices/");
URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{url});
Class service = Class.forName("com.MyClass",true,urlClassLoader );
我收到 java.lang.ClassNotFoundException
如果我再运行该程序一次(在 Eclipse 中),那么它就可以工作。实际上我只需要在 Eclipse 中刷新项目然后它就
可以工作有人看到这个问题了吗
In my program I generate classes dynamically but when I try:
String[] args = {"-d","D:\\path\\build\\classes","-s","D:\\path\\src","http://services.aonaware.com/DictService/DictService.asmx?WSDL"};
WsImport.doMain(args);
URL url = new URL("file:D:/path/build/classes/com/aonaware/services/webservices/");
URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{url});
Class service = Class.forName("com.MyClass",true,urlClassLoader );
I recieve java.lang.ClassNotFoundException
If I run one more time the program (in Eclipse), then it is working. Actually I only have to refresh the project in Eclipse and then its working
Does anybody see the problem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
听起来像是类路径问题。确保相关类(或包含它的 jar)已编译并且位于类路径中。
“动态生成类”到底是什么意思?如果为类生成 java 源文件,则需要先将其编译为类文件,然后类加载器才能获取它。也许 Eclipse 在第二轮中做到了这一点,这就是它当时起作用的原因。
Sounds like a classpath issue. Make sure that the class in question (or the jar containing it) is compiled and is in the classpath.
What exactly do you mean by "generating classes dynamically"? If you generate the java source file for a class, it needs to be compiled first into a class file, before the classloader can pick it up. Maybe Eclipse does that in the second round, that's why it is working then.
您通常会使用
ClassLoader
,例如 URLClassLoader 在运行时动态加载类。You would generally use a
ClassLoader
like URLClassLoader to load classes dynamically at runtime.使用 Class.forName() 的三个参数形式,这需要您指定 ClassLoader。
[Java API 链接][1]
[1]: http://java.sun.com/javase/6/docs/api/java/lang/Class.html#forName(java.lang.String, boolean, java. lang.ClassLoader)
Use the three argument form of Class.forName() which requires you specify the ClassLoader.
[Java API Link][1]
[1]: http://java.sun.com/javase/6/docs/api/java/lang/Class.html#forName(java.lang.String, boolean, java.lang.ClassLoader)
阅读有关
URLClassLoader
的 文档:任何以“/”结尾的 URL 都被假定为引用目录。此外,您可能应该使用“/”作为分隔符,而不是
\\.
附录:
嗯,如果指定为 URL 的目录中有实际编译的 Java 类,那么您的代码对我来说非常适合。但是当你说
生成Java源代码还是直接生成字节码?如果它是源代码,您也会从您的应用程序编译它吗?
Read the docs on
URLClassLoader
: Any URL that ends with a '/' is assumed to refer to a directory.Also, you probably should use '/' as a separator instead of
\\
.Addendum:
Well, your code works perfectly for me -- if there are actual compiled Java classes in the directory specified as an URL. But when you say
do you generate Java source or bytecode directly? If it's source code, do you also compile it from your app?