Class.forName(String ClassName) throwing ClassNotFoundException

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2009
Posts: 39
Reputation: multicoder is an unknown quantity at this point 
Solved Threads: 0
multicoder multicoder is offline Offline
Light Poster

Class.forName(String ClassName) throwing ClassNotFoundException

 
0
  #1
Jun 22nd, 2009
Hi,
My code below -
String parserConfigurationClassName = this.cli.getParserConfigImpl();//Gets implementation's name as a string
Class parserConfigurationClass;
try {
parserConfigurationClass = Class.forName(parserConfigurationClassName);
}
catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Failed to load parserConfigImpl [" + parserConfigurationClassName + "]: Class not found");
}
So as you can see, I am trying to create the object of the class which i get as a string , and try to create the object of that class using Class.forName(String) but it throws ClassNotFoundException.
I go this in the sun's documentation as =>
-----------------------------------------------------------------------------
forName
Throws:
LinkageError - if the linkage fails
ExceptionInInitializerError - if the initialization provoked by this method fails
ClassNotFoundException - if the class cannot be located
-----------------------------------------------------------------------------
So it means if the jvm could not locate the class then it wil throw the class not found exception, but here i can i created the class ans can also see the compiled clasfile for the same, that too in proper directory.
So plz help me toi figure out the reason behind this, which has stopped my development work completely.

Thanks & regards,
Multicoder
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Class.forName(String ClassName) throwing ClassNotFoundException

 
0
  #2
Jun 22nd, 2009
The class was loaded in another ClassLoader, seemingly, and now you are trying to load it using the context classloader. That doesn't work.

The thread that is doing this will need to use the setContextClassLoader (see Thread) method, and the getClass().getClassLoader() method of the "loaded" class. Then this should work.

The real question is, though, why do you need the Class at this point? If this is some sort of plugin architecture, then I can only assume that this Class (the one that is attempting to load the other), is part of the "framework". And, if that is so, it should only be working within the defined interface that the plugin is to adhere to.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 39
Reputation: multicoder is an unknown quantity at this point 
Solved Threads: 0
multicoder multicoder is offline Offline
Light Poster

Re: Class.forName(String ClassName) throwing ClassNotFoundException

 
0
  #3
Jun 22nd, 2009
thnx masijade!
the first immediate and nice reply i got!!
I was suspecting it got something to do with classloader, but not sure how to handle this one.
Plz look for the post here,m trying to think as u say...
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 39
Reputation: multicoder is an unknown quantity at this point 
Solved Threads: 0
multicoder multicoder is offline Offline
Light Poster

Re: Class.forName(String ClassName) throwing ClassNotFoundException

 
0
  #4
Jun 22nd, 2009
Hi,
I checked with setContextClassLoader(), but to tel u i m not creating any extra thread.
Only the main thread running all around the execution and the class for which i am trying to load is part of the application framework,
But which class to load is passed as cmd param and accordignly need to create the object of that class.
So I cant figure out any issue with the class loader..??
plz clarify me...!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Class.forName(String ClassName) throwing ClassNotFoundException

 
0
  #5
Jun 22nd, 2009
Change that catch block to also do a printStackTrace() and then post the complete exception.

Also post the "classpath" in use (most likely not the System classpath). Then also post the full path to the class to be loaded.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 39
Reputation: multicoder is an unknown quantity at this point 
Solved Threads: 0
multicoder multicoder is offline Offline
Light Poster

Re: Class.forName(String ClassName) throwing ClassNotFoundException

 
0
  #6
Jun 22nd, 2009
hi,
I believe the exception.printStackTrace()
prints the the contents of the system.err to the standard error output system i.e. to cmd
so is this right?then this is my output on cmd -
Error: Failed to load parserConfigImpl [au.com.allhomes.listing.harvester.parse.
RealEstateCoParserConfiguration]: Class not found
java ListingTestHarness <options>
Where options include:
--inputHtml <htmlFile> The HTML file to parse.
--listingType <residentialSale | commercialSale | businessSale | ruralSale |
residentialRental | commercialRental>
--parserConfigImpl <parserConfigImpl> The parser configuration to use.
--parserClassImpl <parserClassImpl> [Optional]: The parser class to use.

--urlConfigFile <urlConfigFile> A mapping of remote URLs to local te
st resources.
--pageType <listingPage | searchPage> The type of HTML page being harvested.
classpath at the point of execution (i.e.path for the class where the above code gets called) is -
com.allhomes.listing.harvester.harness.ListingTestHarnessConfiguration.java
and the classpath to the class which trying to get load is -
com.allhomes.listing.harvester.parse.RealEstateCoParserConfiguration.java
i think this will reveal the package structure also..

waiting for ur reply
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,028
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 151
JamesCherrill JamesCherrill is offline Offline
Veteran Poster

Re: Class.forName(String ClassName) throwing ClassNotFoundException

 
0
  #7
Jun 22nd, 2009
Error: Failed to load parserConfigImpl [au.com.allhomes.listing.harvester.parse.

Maybe the au. is significant?
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Class.forName(String ClassName) throwing ClassNotFoundException

 
0
  #8
Jun 22nd, 2009
The au is definately significant, but the stuff printed is neither the "printstacktrace" that was asked for, nor the classpath, nor the fullpath to the classfile.

@OP before you start playing around with classloaders and reflection learn what the classpath is, and how to use it, and learn the "tools" (i.e. java and javac) and the options (and their effects) that affect the classpath.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 39
Reputation: multicoder is an unknown quantity at this point 
Solved Threads: 0
multicoder multicoder is offline Offline
Light Poster

Re: Class.forName(String ClassName) throwing ClassNotFoundException

 
0
  #9
Jun 22nd, 2009
Originally Posted by masijade View Post
The au is definately significant

The au seem to be significant here because my mistake in providing the package structure, but its not actually!
I configured the project in netbeans again to see it holds the parent directory "au" also.
So 'au' is the parent directory for all and inside it starts from 'com'.
I executed again but got the same exception.

Originally Posted by masijade View Post
@OP before you start playing around with classloaders and reflection learn what the classpath is, and how to use it, and learn the "tools" (i.e. java and javac) and the options (and their effects) that affect the classpath.

I know very wel abt both the tools but m not executing it from netbeans so that i can set the parameters i need to pass each time easily.
So at the cmd only moving to project's source directory (which is C:\Harvester) where the netbeans is putting compiled code and
not playing around with the classapth.
I know the significance of classpath.
Plz correct me further?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,028
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 151
JamesCherrill JamesCherrill is offline Offline
Veteran Poster

Re: Class.forName(String ClassName) throwing ClassNotFoundException

 
0
  #10
Jun 22nd, 2009
Don't know about netbeans, but eclipse uses its own class loader and causes all kinds of problems like yours - except that the prog runs OK if executed via java or javaw. Maybe worth trying outside netbeans?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2662 | Replies: 26
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC