1

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
  • Do you generate Java source files that have to be compiled (and do you compile it yourself?) or do you generate binary class files? Commented Mar 29, 2010 at 9:46
  • No, I dont generate alone the source files and I dont compile alone. As I pasted the code up in the question, I use the tool "wsimport" which creates java classes from a WSDL and also compiles them alone. Commented Mar 29, 2010 at 19:29

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I was already thinking that is a classpath problem? How I can solve it?
@Milan: Well for one thing you could tell us how you're running the program from the command line and from Eclipse.
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.
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
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/");
|
1

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

1 Comment

I tried with URLClassLoader but probably I make some mistake. Ill paste the code in the question
1

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)

1 Comment

I tried also that way with three argument method but again the same java.lang.ClassNotFoundException
1

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?

2 Comments

I changed to '/' but still java.lang.ClassNotFoundException
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.