• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Dynamic addURL appears to fail

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to write code so that no matter what the path is, the relative directories with configuration files and supporting jar libraries will be added at the application invocation. (This has to work from inside the eclipse framework AND in a production build).

Development Structure:

HelloWorld(Project)
/classes
HelloWord.class
/config
application.properties
/lib

Production Structure:
HelloWorld.jar
/config
application.properties
/lib
neededJar.jar

The URL for the properties is: file:/C:/HelloWorld/config/application.properties (dynamically created by using File to find config and then toUri().toURL()
Similar approach for the jar.

These are returned from createURLS() [method not shown here].

The following code snippet (minus the try/catch) is then used to attempt to add to the classloader's classpath:
--------------------------------------------------------------------------------------------------------------------------------
ArrayList<URL> list = createURLS();

Method [] methods = URLClassLoader.class.getDeclaredMethods();
Method getURLs = URLClassLoader.class.getDeclaredMethod("getURLs", null); //used to check for urls added
Method addURL = URLClassLoader.class.getDeclaredMethod("addURL", new Class [] {URL.class}); //used to add the urls

getURLs.setAccessible(true);
addURL.setAccessible(true);


Object a = getURLs.invoke(HelloWorld.class.getClassLoader(), null); //see what urls are present before adding

for(URL url: list)
{
addURL.invoke(HelloWorld.class.getClassLoader(), url);
}

a = getURLs.invoke(HelloWorld.class.getClassLoader(), null); //confirm that urls are added

URL url = HelloWorld.class.getResource("application.properties");

-------------------------------------------------------------------------------------------
end snippet

The url variable returns NULL!

I do not understand why this tried approach is not working! Any suggestions or clarifications?

Please accept my thanks for your time in responding in advance!!

JD

 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please Use Code Tags.
 
J.D. Thompson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about that, I thought the dashed line with code snippet would work, here is the portion that the tags would apply to:



Thank you!

JD
 
J.D. Thompson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, have solved the issue. The URL passed to add needs to be the containing structure, not the resource itself, i.e.

file:/C:/HelloWorld/config/

instead of

file:/C:/HelloWorld/config/application.properties


hope this helps anyone else attempting to dynamically add to the classpath!!!
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic