views:

180

answers:

4

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

+2  A: 

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.

Péter Török
I was already thinking that is a classpath problem?How I can solve it?
Milan
@Milan: Well for one thing you could tell us how you're running the program from the command line and from Eclipse.
Jon Skeet
Well in the program I use the tool: wsimport which creates src files and compile them. Also when I look in the src folder and build/classes folder in the Eclipse project, the files are there.
Milan
The src-Folders are usually *not* at the classpath in Eclipse. When refreshing Eclipse, it just compiles the .java files to the bin/classes directory. Use your bin/classes directory as target for generated sources
Hardcoded
Hm... How you mean to use bin/classes directory as target for generated sources? I does it. or not? I have this: URL url = new URL("file:D:/path/build/classes/com/aonaware/services/webservices/");
Milan
Ahaaaa, I found the problem: The problem was in the path: I had to be: URL url = new URL("file:D:/path/build/classes/");Thank you everybody for the help! Cheers! :p
Milan
+1  A: 

You would generally use a ClassLoader like URLClassLoader to load classes dynamically at runtime.

McDowell
I tried with URLClassLoader but probably I make some mistake.Ill paste the code in the question
Milan
A: 

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)

Tim Bender
I tried also that way with three argument method but again the same java.lang.ClassNotFoundException
Milan
A: 

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

In my program I generate classes dynamically

do you generate Java source or bytecode directly? If it's source code, do you also compile it from your app?

Robert Petermeier
I changed to '/' but still java.lang.ClassNotFoundException
Milan
As I pasted the code in the question, I use the tool "wsimport" that generates source code from WSDL and also alone it compiles it.
Milan