Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Runtime Class Loading in Servlets  RSS feed

 
nishith pandya
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is necessary for my servlet to compile some java files and right after to instantiate one of the new classes the servlet compiles.




When I try to instantiate one of the classes, I get the following exception :
java.lang.ClassNotFoundException: Prof_X.holders.ApiValidateStyleHolder
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1340)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1189)
at com.spearhead.webservices.servlets.WSDLMainServlet.doPost(WSDLMainServlet.java:112)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:768)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:861)




I guess the new classes have not been loaded ?





Here is a code snippet from my servlet's doPost :

public void doPost(HttpServletRequest request, HttpServletResponse response) {
//compiling the java files
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("javac -d ../webapps/wsdlTester/WEB-INF/classes Prof_X/holders/*.java");

//creating new instance of one of the newly compiled classes
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Class myClass=loader.loadClass("Prof_X.holders.ApiValidateStyleHolder");
ApiValidateStyleHolder holder = new ApiValidateStyleHolder();

}



Does anyone know what I should do ?

Thanks,

Nish
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you verified that the class is being created and that it is the correct location? You may want to pipe the javac output to the log file to verify the compilation is working.
 
nishith pandya
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,


Yes...I do see the .class file :

C:\jakarta-tomcat-5.0.30\webapps\wsdlTester\WEB-INF\classes\Prof_X\holders\ApiValidateStyleHolder.class
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a few possibilities I can think of, but you're going to need to play around a little more. I'm not convinced this belongs in the Servlets forum so it may be moved to another forum soon.

Are you sure only a single thread is executing? Since the destination file is absolute you could get problems if it gets written twice concurently. Likewise, are the compilation and loading on the same thread? If not, it may be getting loaded before it is ready. You may want to 'waitFor()' the Process if you are not doing so already.

Possibly the first step is to use the generated class in a unit test outside the servlet container, make sure the class is valid, since invalid class files can also cause ClassNotFound exceptions. If you're using JDK1.4 or later you may want to check if the exception has a getCause() available.

Is this class supposed to be generated once or multiple times? I'm sure you have your reasons for runtime class generation, but it seems a bit wild and crazy at first glance. If you need to refresh the class you will need to wwrap it in its own ClassLoader so you can unload the Class.

Hope this helps, tell us how you go.

Dave.
 
nishith pandya
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,

I think I might have narrowed the problem..but I dont think its solved.

The class that I try to load does have imports of other classes that I have yet to load.

I tried a similar test by compiling a Test.java that has a package structure from within the servlet. The Test.java is a bare bones class. I was then able to load the Test class fine.

So..the only thing that is different from this Test and what I was trying to load that gave me the error is that the class I was trying to load had imports.


I will try and invsestigate this further since I dont know how to do this curently.

Thanks,

Nish
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!