aspose file tools*
The moose likes Java in General and the fly likes Using ClassLoaders to load multiple classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using ClassLoaders to load multiple classes" Watch "Using ClassLoaders to load multiple classes" New topic
Author

Using ClassLoaders to load multiple classes

Femi Alla
Ranch Hand

Joined: Jul 05, 2002
Posts: 79
Hello,
I succesfully used the URLClassLoader to load a class unto the current program I was running but couldn't get to load multiple classes. The directory structure of the classes I'm trying to load is as follows: TheMultitreadedExperiment is a folder that contains "TheMainThread.java". It calls up 2 other threads in it's subfolders "ThreadTester1", and "ThreadTester2" which contains classes that are named accordingly. I placed the program (ClassLoaderTest.java)I was unsing to load the other classes in the the directory immediately above the the MultithreadedExperiment in the directory hirerchy i.e the direcory in which ClassLoaderTest resides is just outside/contains TheMultiThreadedExperiment folder. I now went ahead to load the class MainThread unto ClassLoaderTest and it worked, but while the program was executing, when it got to the point where it's supposed to call up the others,[The program works normally with the invocation of the MainThread class on the command line, through the use of package statements] the program just terminates. This then lead me to try to load the other programs in the subdirectories unto ClassLoaderTest.java but couldn't figure out how to do this. I looked up the docs and I found a method in the URLClassLoader class that I think should do the work but can't figure out how to use it. It's findClass(). This is how the docs describe findClass
findClass
protected ClassfindClass(String name)
throws ClassNotFoundException
Finds and loads the class with the specified name from the URL search path. Any URLs referring to JAR files are loaded and opened as needed until the class is found.
Overrides:
findClass in class ClassLoader
Parameters:
name - the name of the class
Returns:
the resulting class
Can anyone please take it up from here? I need help here and any suggestion would be appreciated.
Thanks.

This is the code that worked....till it had to call up the other classes outside/imported in "MainThread.java".

This is my attempt to load the other classes:

Resulting error messages:
C:\JavaPrograms\PersonalPractice\PersonalExperiments>java ClassLoaderTest
An Exception was caught while trying to load your class(es). Details:java.net.Ma
lformedURLException: unknown protocol: file:C:\JavaPrograms\PersonalPractice\Per
sonalExperiments/TheMultithreadedExperiment/
PS: Please tell me if I put up this question in an inappropriate forum ..i.e this question was supposed to be posted in the advanced section.

Thanks again.


SCJP
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using ClassLoaders to load multiple classes