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

Edited 3 Years Ago by mike_2000_17: Fixed formatting

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.

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

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...!

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.

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

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.

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.

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

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?

He says he is trying it from outside of the IDE.

@OP, you are obviously contradicting yourself when you say you know the basics of the classpath then talk about moving to the source directory. You also contradict yourself about those points when I ask you for the classpath, and you give me a package path style reference to a source file, then do the same thing when I ask after the full filepath of the actual class (not source file) to be loaded.

Now, provide me with the classpath you use to execute the command (if none, say none), and provide me with the full classname with package (class name not source file name), and provide me with the full filepath to the class to be loaded (full filepath). Also, provide me with the full filepath to the directory in which you find yourself when you execute the command, and provide the full and exact command you used to execute the command, and provide me with the full stacktrace, not your own generated error message.

provide me with the classpath you use to execute the command (if none, say none),

I am not providing any classpath to the cmd.this is the command m hitting with cmd -

provide me with the full classname with package (class name not source file name),

C:\Harvester\build\classes\au\com\allhomes\listing\harvester\harness\ListingTestHarnessConfiguration.class

provide me with the full filepath to the class to be loaded (full filepath).

C:\Documents and Settings\pritam\Desktop\au\com\allhomes\listing\harvester\parse\RealEstateCoParserConfiguration.java

provide me with the full filepath to the directory in which you find yourself when you execute the command,

Executing the (above) said command from 'C:\'

and provide the full and exact command you used to execute the command,

java -Xmx512m au.com.allhomes.listing.harvester.harness.ListingTestHarness --inputHtml /Harvester/build/classes/sample/data/rsearch.html --listingType residentialSale --parserConfigImpl au.com.allhomes.listing.harvester.parse.RealEstateCoParserConfiguration --urlConfigFile /Harvester/build/classes/sample/data/local_url_map.properties --outputXml results-realestate.xml --pageType listingPage And getting the anything from printStackTrace , tried below code -

StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);

e.printStackTrace(pw);
            e.getStackTrace();
           System.out.println("Error = " + sw.toString());
         throw new IllegalArgumentException("Failed to load** parserConfigImpl [" + parserConfigurationClassName + "]: Class not found");

I am not providing any classpath to the cmd.this is the command m hitting with cmd -

Okay, so what is the value of the System CLASSPATH environment variable?

C:\Harvester\build\classes\au\com\allhomes\listing\harvester\harness\ListingTestHarnessConfiguration.class

Here I wanted the class name (e.g. au.com.allhomes.listing.harvester.harness.ListingTestHarnessConfiguration)

C:\Documents and Settings\pritam\Desktop\au\com\allhomes\listing\harvester\parse\RealEstateCoParserConfiguration.java

Compiled class filepath not the source file path, I couldn't, to tell you the truth, care less about the source file at this point, but I assume the above (where I wanted the classname) is it, and I will assume the classname I provided is correct.

Executing the (above) said command from 'C:\'

Well, if you are executing directly from C:\, and not from C:\Harvester\build\classes\ (assuming '.' is in the System CLASSPATH), then of course it is ClassNotFound (depending on what the system classpath is). You should actually be getting a ClassNotFound on the "main" class.

java -Xmx512m au.com.allhomes.listing.harvester.harness.ListingTestHarness --inputHtml /Harvester/build/classes/sample/data/rsearch.html --listingType residentialSale --parserConfigImpl au.com.allhomes.listing.harvester.parse.RealEstateCoParserConfiguration --urlConfigFile /Harvester/build/classes/sample/data/local_url_map.properties --outputXml results-realestate.xml --pageType listingPage And getting the anything from printStackTrace , tried below code -

StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);

e.printStackTrace(pw);
            e.getStackTrace();
           System.out.println("Error = " + sw.toString());
         throw new IllegalArgumentException("Failed to load** parserConfigImpl [" + parserConfigurationClassName + "]: Class not found");

why not simply

e.printStackTrace();
throw new IllegalArgumentException("Failed to load** parserConfigImpl [" + parserConfigurationClassName + "]: Class not found");

and let the stacktrace print to STDERR. It's only to get the complete stacktrace, and you can, if you want, remove it later.

Okay, so what is the value of the System CLASSPATH environment variable?

C:\Program Files\Java\jdk1.6.0_14\lib;
this is System CLASSPATH environment variable

Here I wanted the class name (e.g. au.com.allhomes.listing.harvester.harness.ListingTestHarnessConfiguration)

Thnx. i got it now, u mean complete class name like u said above

Compiled class filepath not the source file path, I couldn't, to tell you the truth, care less about the source file at this point, but I assume the above (where I wanted the classname) is it, and I will assume the classname I provided is correct.

The complete path to the class files(compiled files and with extension .class)is like C:\Harvester\build\classes\au\com\allhomes\listing\harvester

Well, if you are executing directly from C:\, and not from C:\Harvester\build\classes\ (assuming '.' is in the System CLASSPATH), then of course it is ClassNotFound (depending on what the system classpath is). You should actually be getting a ClassNotFound on the "main" class.

there I put ';' in the system classpath but what is the significance of '.' in the System CLASSPATH ?
and how the user vairable 'classpath' and the 'CLASSPATH' (its in capital, which i get to see often) from System variables list below it...?Plz explain me this too,even if its the out of scope of the post.

why not simply

e.printStackTrace();
throw new IllegalArgumentException("Failed to load** parserConfigImpl [" + parserConfigurationClassName + "]: Class not found");

and let the stacktrace print to STDERR. It's only to get the complete stacktrace, and you can, if you want, remove it later.

I tried with the same but I am not getting apart from the

Error: Cannot read the urlConfigFile [/Harvester/build/classes/sample/data/local
_url_map.properties]
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.

Also I noticed one thing with the netbeans behavour, which is very strange for me.
If I change the code, like the test/string inside

throw new IllegalArgumentException("Failed to load** parserConfigImpl [" + parserConfigurationClassName + "]: Class not found");

Like as u can see i put the ** in the string and compiled the project again.Now if i execute it from the cmd i cant see the change in the output exception i got.
what wrong with my concepts..??

'.' is the "current" directory symbol.

How to get the value of an environment variable is an OS question, but for Windows type "set CLASSPATH" (and it should be uppercase not lowercase).

As far as the error posted above, that is not one that has to do with the problem you say you've been having, and to solve that one does C:\Harvester\build\classes\sample\data\local exist? Is it locked? Do you have permission to it?

Also I noticed one thing with the netbeans behavour, which is very strange for me.
If I change the code, like the test/string inside

throw new IllegalArgumentException("Failed to load** parserConfigImpl [" + parserConfigurationClassName + "]: Class not found");

Like as u can see i put the ** in the string and compiled the project again.Now if i execute it from the cmd i cant see the change in the output exception i got.
what wrong with my concepts..??

And if you are getting the error you posted directly before this post, then look at that error, as it is a completely different error than you have been getting.

Thnx.Yes i had noticed this thing before.I used to get the exception cannot find the file local_url_propeties because i had moved the folder data for some experimenting purpose.
And when i realize this i kept it back.Now I had did it again and resolved with that prev error.
The error I am facing now is this exception I am talking about -

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.

au.com.allhomes.listing.harvester.parse.
RealEstateCoParserConfiguration is my implementation of the
interface which I need to pass as a 'parserConfigImpl' parameter to the cmd.
But it throws the exception classNotFound..??
sorry if i confused u between these both errors.

Lets try to cut this short. Execute the command as follows:

java -Xmx512m -cp C:\Harvester\build\classes au.com.allhomes.listing.harvester.harness.ListingTestHarness ...

or

cd C:\Harvester\build\classes
java -Xmx512m -cp . au.com.allhomes.listing.harvester.harness.ListingTestHarness ...

Lets try to cut this short. Execute the command as follows:

java -Xmx512m -cp C:\Harvester\build\classes au.com.allhomes.listing.harvester.harness.ListingTestHarness ...

or

cd C:\Harvester\build\classes
java -Xmx512m -cp . au.com.allhomes.listing.harvester.harness.ListingTestHarness ...

For this I got this -

C:\>java -Xmx512m -cp  au.com.allhomes.listing.harvester.harness.ListingTestHarn
ess --inputHtml /Harvester/build/classes/sample/data/rsearch.html --listingType
residentialSale --parserConfigImpl au.com.allhomes.listing.harvester.parse.RealE
stateCoParserConfiguration --urlConfigFile /Harvester/build/classes/sample/data/
local_url_map.properties --outputXml results-realestate.xml --pageType listingPa
ge
Unrecognized option: --inputHtml
Could not create the Java virtual machine.

Got this after above -

Exception in thread "main" java.lang.NoClassDefFoundError: au/com/allhomes/listi
ng/harvester/harness/ListingTestHarness
Caused by: java.lang.ClassNotFoundException: au.com.allhomes.listing.harvester.h
arness.ListingTestHarness
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: au.com.allhomes.listing.harvester.harness.Listing
TestHarness.  Program will exit.

By the first one, you did not enter the classpath.

By the second one, you seem to have a line break in the classname (cut-n-paste error).

Don't just print the error, print the command you used as well, and make sure you get the command right.

I mean to say, I tried -
Case I)
C:\>java -Xmx512m -cp C:\Harvester\build\classes\au.com.allhomes.listing.harvest
er.harness.ListingTestHarness --inputHtml /Harvester/build/classes/sample/data/r
search.html --listingType residentialSale --parserConfigImpl au.com.allhomes.lis
ting.harvester.parse.RealEstateCoParserConfiguration --urlConfigFile /Harvester/
build/classes/sample/data/local_url_map.properties --outputXml results-realestat
e.xml --pageType listingPage
Unrecognized option: --inputHtml
Could not create the Java virtual machine.

Case II)
C:\>cd C:\Harvester\build\classes

C:\Harvester\build\classes>java -Xmx512m -cp . au.com.allhomes.listing.harvester
.harness.ListingTestHarness --inputHtml /Harvester/build/classes/sample/data/rse
arch.html --listingType residentialSale --parserConfigImpl au.com.allhomes.listi
ng.harvester.parse.RealEstateCoParserConfiguration --urlConfigFile /Harvester/bu
ild/classes/sample/data/local_url_map.properties --outputXml results-realestate.
xml --pageType listingPage
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - cannot find sy
mbol
symbol : class HarvesterConfigurationStub
location: package au.com.allhomes.listing.harvester
at au.com.allhomes.listing.harvester.harness.ListingTestHarness.<clinit>
(ListingTestHarness.java:11)
Could not find the main class: au.com.allhomes.listing.harvester.harness.Listing
TestHarness. Program will exit.

I think u mean -
java -XmX512m -cp C:\Harvester\build\classes au.com....

That is, space after C:\Harvester\build\classes to provide the classpath.
So for this i got below thing -

C:\>java -Xmx512m -cp C:\Harvester\build\classes au.com.allhomes.listing.harvest
er.harness.ListingTestHarness --inputHtml /Harvester/build/classes/sample/data/r
search.html --listingType residentialSale --parserConfigImpl au.com.allhomes.lis
ting.harvester.parse.RealEstateCoParserConfiguration --urlConfigFile /Harvester/
build/classes/sample/data/local_url_map.properties --outputXml results-realestat
e.xml --pageType listingPage
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - cannot find sy
mbol
symbol : class HarvesterConfigurationStub
location: package au.com.allhomes.listing.harvester
at au.com.allhomes.listing.harvester.harness.ListingTestHarness.<clinit>
(ListingTestHarness.java:11)
Could not find the main class: au.com.allhomes.listing.harvester.harness.Listing
TestHarness. Program will exit.

Also for 2nd one, i put -
C:\>cd C:\Harvester\build\classes

C:\Harvester\build\classes>java -Xmx512m -cp . au.com.allhomes.listing.harvester
.harness.ListingTestHarness --inputHtml /Harvester/build/classes/sample/data/rse
arch.html --listingType residentialSale --parserConfigImpl au.com.allhomes.listi
ng.harvester.parse.RealEstateCoParserConfiguration --urlConfigFile /Harvester/bu
ild/classes/sample/data/local_url_map.properties --outputXml results-realestate.
xml --pageType listingPage
I suppose there is a '.' to signify the current working directory as a classpath and I am getting this -

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - cannot find sy
mbol
symbol : class HarvesterConfigurationStub
location: package au.com.allhomes.listing.harvester
at au.com.allhomes.listing.harvester.harness.ListingTestHarness.<clinit>
(ListingTestHarness.java:11)
Could not find the main class: au.com.allhomes.listing.harvester.harness.Listing
TestHarness. Program will exit.

I think u mean -
java -XmX512m -cp C:\Harvester\build\classes au.com....

That is, space after C:\Harvester\build\classes to provide the classpath.
So for this i got below thing -

Of course I did, seeing as how I entered it that way. You copied it wrong.

I suppose there is a '.' to signify the current working directory as a classpath and I am getting this -

Of course, as mentioned earlier.

You are now getting a different error. This problem is solved.

Start a new thread with this new problem.

Its just that we got rid of that exception, but in efforts to that we created a different problem.now we are playing to change the classpath in the command itself and the classloader can now create the object of 'RealEstateCoParserConfiguration' i.e. my implementation for the interface.
But in the process alters the classpath and cannot find the succeeding classes.
Is this right?or else i should post the new question for these classes can not find error...?Since now m getting cannot find symbol HarvesterConfigurationStub and getting exception at main

No we did not create a problem, simply got the code to run far enough for that already existing problem to be noticed. This thread is already too long IMHO, and this current error has nothing to do with the original error, so it is time for a new thread.

You are not "creating" any classpaths in your code, you are simply executing the program with a classpath so that it can find anything at all. It now looks as though you are attempting to compile some code within your code, which, as already said, is a new problem.

You are, however, in really very far over your head attemtping this SOS with the level of knowledge you've shown here for the basic tenants of the language that every programmer should learn as one of their first lessons.

Comments
Sounds very knowledgeble person
This article has been dead for over six months. Start a new discussion instead.