943,917 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 9615
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 22nd, 2009
0

Class.forName(String ClassName) throwing ClassNotFoundException

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Light Poster
multicoder is offline Offline
39 posts
since Jun 2009
Jun 22nd, 2009
0

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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jun 22nd, 2009
0

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...
Reputation Points: 10
Solved Threads: 0
Light Poster
multicoder is offline Offline
39 posts
since Jun 2009
Jun 22nd, 2009
0

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...!
Reputation Points: 10
Solved Threads: 0
Light Poster
multicoder is offline Offline
39 posts
since Jun 2009
Jun 22nd, 2009
0

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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jun 22nd, 2009
0

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
Reputation Points: 10
Solved Threads: 0
Light Poster
multicoder is offline Offline
39 posts
since Jun 2009
Jun 22nd, 2009
0

Re: Class.forName(String ClassName) throwing ClassNotFoundException

Error: Failed to load parserConfigImpl [au.com.allhomes.listing.harvester.parse.

Maybe the au. is significant?
Featured Poster
Reputation Points: 1924
Solved Threads: 951
Posting Expert
JamesCherrill is offline Offline
5,788 posts
since Apr 2008
Jun 22nd, 2009
0

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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jun 22nd, 2009
0

Re: Class.forName(String ClassName) throwing ClassNotFoundException

Click to Expand / Collapse  Quote originally posted by masijade ...
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.

Click to Expand / Collapse  Quote originally posted by masijade ...
@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?
Reputation Points: 10
Solved Threads: 0
Light Poster
multicoder is offline Offline
39 posts
since Jun 2009
Jun 22nd, 2009
0

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?
Featured Poster
Reputation Points: 1924
Solved Threads: 951
Posting Expert
JamesCherrill is offline Offline
5,788 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: null pointer exception problem
Next Thread in Java Forum Timeline: How can login the remote system using java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC