在运行时在java中创建类的实例 [英] Make instance of class in java at runtime

查看:118
本文介绍了在运行时在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中刷新项目,然后工作



有人看到这个问题

听起来像一个类路径问题。确保所讨论的类(或包含它的jar)被编译并且在类路径中。



动态生成类到底是什么意思?如果你为一个类生成java源文件,它需要首先被编译成一个类文件,在classloader可以拾取它之前。也许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

解决方案

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.

这篇关于在运行时在java中创建类的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆