• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using ClassLoaders to load multiple classes

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Lasagna is spaghetti flvored cake. Just like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic