DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Class.forName(String ClassName) throwing ClassNotFoundException (http://www.daniweb.com/forums/thread198990.html)

multicoder Jun 22nd, 2009 7:26 am
Class.forName(String ClassName) throwing ClassNotFoundException
 
Hi,
My code below :) -
Quote:

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

masijade Jun 22nd, 2009 7:45 am
Re: Class.forName(String ClassName) throwing ClassNotFoundException
 
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.

multicoder Jun 22nd, 2009 7:50 am
Re: Class.forName(String ClassName) throwing ClassNotFoundException
 
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...

multicoder Jun 22nd, 2009 7:57 am
Re: Class.forName(String ClassName) throwing ClassNotFoundException
 
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...!

masijade Jun 22nd, 2009 8:04 am
Re: Class.forName(String ClassName) throwing ClassNotFoundException
 
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.

multicoder Jun 22nd, 2009 8:32 am
Re: Class.forName(String ClassName) throwing ClassNotFoundException
 
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 -
Quote:

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 -
Quote:

com.allhomes.listing.harvester.harness.ListingTestHarnessConfiguration.java
and the classpath to the class which trying to get load is -
Quote:

com.allhomes.listing.harvester.parse.RealEstateCoParserConfiguration.java
i think this will reveal the package structure also..

waiting for ur reply

JamesCherrill Jun 22nd, 2009 8:49 am
Re: Class.forName(String ClassName) throwing ClassNotFoundException
 
Error: Failed to load parserConfigImpl [au.com.allhomes.listing.harvester.parse.

Maybe the au. is significant?

masijade Jun 22nd, 2009 9:22 am
Re: Class.forName(String ClassName) throwing ClassNotFoundException
 
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.

multicoder Jun 22nd, 2009 10:10 am
Re: Class.forName(String ClassName) throwing ClassNotFoundException
 
Quote:

Originally Posted by masijade (Post 896822)
The au is definately significant

:S
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.

Quote:

Originally Posted by masijade (Post 896822)
@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.

:X
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?:idea:

JamesCherrill Jun 22nd, 2009 10:27 am
Re: Class.forName(String ClassName) throwing ClassNotFoundException
 
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?


All times are GMT -4. The time now is 2:21 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC