Prev: Runtime.getRuntime().exec
Next: get file version
From: mike on 20 Apr 2010 04:04 Hi, I have exteneded URLClassLoader to: import java.net.URL; import java.net.URLClassLoader; /** * The ExtenedClassLoader class. */ public class ExtendedClassLoader extends URLClassLoader { /** * @param urls, to carryforward the existing classpath. */ public ExtendedClassLoader(URL[] urls) { super(urls); } @Override /** * add ckasspath to the loader. */ public void addURL(URL url) { super.addURL(url); } } Then to add my jar file to classpath at runtime I use the following code ( see below). private void addToClasspath(){ try { URL urls [] = {}; String path = "/home/xddmid/out/xml.jar"; String urlPath = "jar:file://" + path + "!/"; URL url = new URL (urlPath); ExtendedClassLoader cl = new ExtendedClassLoader (urls); cl.addURL(url); String resource = ClassLoader.getSystemClassLoader().getResource("test.xml").toExternalForm(); System.out.println ("Success! Resource "+resource + " found!"); } catch (Exception ex) { System.out.println ("Failed."); ex.printStackTrace (); } I have verified that the jarfile exists and that it contains "test.xml file" ( "jar tvf xml.jar). The "test.xml" file is directly in root ( no package name). I get a NullPointerException for this line: String resource = ClassLoader.getSystemClassLoader().getResource("test.xml").toExternalForm(); Any help or ideas are very much appreciated. br, //mike
From: Alessio Stalla on 20 Apr 2010 04:50 On Apr 20, 10:04 am, mike <mikaelpetter...(a)hotmail.com> wrote: > Hi, > > I have exteneded URLClassLoader to: > > import java.net.URL; > import java.net.URLClassLoader; > > /** > * The ExtenedClassLoader class. > */ > public class ExtendedClassLoader extends URLClassLoader { > /** > * @param urls, to carryforward the existing classpath. > */ > public ExtendedClassLoader(URL[] urls) { > super(urls); > } > > @Override > /** > * add ckasspath to the loader. > */ > public void addURL(URL url) { > super.addURL(url); > } > > } > > Then to add my jar file to classpath at runtime I use the following > code ( see below). > > private void addToClasspath(){ > try > { > URL urls [] = {}; > > String path = "/home/xddmid/out/xml.jar"; > String urlPath = "jar:file://" + path + "!/"; > URL url = new URL (urlPath); > ExtendedClassLoader cl = new ExtendedClassLoader (urls); > cl.addURL(url); > String resource = > ClassLoader.getSystemClassLoader().getResource("test.xml").toExternalForm(); > System.out.println ("Success! Resource "+resource + " > found!"); > } > catch (Exception ex) > { > System.out.println ("Failed."); > ex.printStackTrace (); > } > > I have verified that the jarfile exists and that it contains "test.xml > file" ( "jar tvf xml.jar). > The "test.xml" file is directly in root ( no package name). > > I get a NullPointerException for this line: > > String resource = > ClassLoader.getSystemClassLoader().getResource("test.xml").toExternalForm(); Why are you using the system classloader and not the one you wrote? In other words, that should be cl.getResource... Alessio
From: mike on 20 Apr 2010 05:24 On 20 Apr, 10:50, Alessio Stalla <alessiosta...(a)gmail.com> wrote: > On Apr 20, 10:04 am, mike <mikaelpetter...(a)hotmail.com> wrote: > > > > > Hi, > > > I have exteneded URLClassLoader to: > > > import java.net.URL; > > import java.net.URLClassLoader; > > > /** > > * The ExtenedClassLoader class. > > */ > > public class ExtendedClassLoader extends URLClassLoader { > > /** > > * @param urls, to carryforward the existing classpath. > > */ > > public ExtendedClassLoader(URL[] urls) { > > super(urls); > > } > > > @Override > > /** > > * add ckasspath to the loader. > > */ > > public void addURL(URL url) { > > super.addURL(url); > > } > > > } > > > Then to add my jar file to classpath at runtime I use the following > > code ( see below). > > > private void addToClasspath(){ > > try > > { > > URL urls [] = {}; > > > String path = "/home/xddmid/out/xml.jar"; > > String urlPath = "jar:file://" + path + "!/"; > > URL url = new URL (urlPath); > > ExtendedClassLoader cl = new ExtendedClassLoader (urls); > > cl.addURL(url); > > String resource = > > ClassLoader.getSystemClassLoader().getResource("test.xml").toExternalForm(); > > System.out.println ("Success! Resource "+resource + " > > found!"); > > } > > catch (Exception ex) > > { > > System.out.println ("Failed."); > > ex.printStackTrace (); > > } > > > I have verified that the jarfile exists and that it contains "test.xml > > file" ( "jar tvf xml.jar). > > The "test.xml" file is directly in root ( no package name). > > > I get a NullPointerException for this line: > > > String resource = > > ClassLoader.getSystemClassLoader().getResource("test.xml").toExternalForm(); > > Why are you using the system classloader and not the one you wrote? In > other words, that should be cl.getResource... > > Alessio This line is used in code to access system resources. I cannot change it. ClassLoader.getSystemClassLoader().getResource("test.xml").toExternalForm(); //mike
From: Alessio Stalla on 20 Apr 2010 09:21 On Apr 20, 11:24 am, mike <mikaelpetter...(a)hotmail.com> wrote: > On 20 Apr, 10:50, Alessio Stalla <alessiosta...(a)gmail.com> wrote: > > > > > On Apr 20, 10:04 am, mike <mikaelpetter...(a)hotmail.com> wrote: > > > > Hi, > > > > I have exteneded URLClassLoader to: > > > > import java.net.URL; > > > import java.net.URLClassLoader; > > > > /** > > > * The ExtenedClassLoader class. > > > */ > > > public class ExtendedClassLoader extends URLClassLoader { > > > /** > > > * @param urls, to carryforward the existing classpath. > > > */ > > > public ExtendedClassLoader(URL[] urls) { > > > super(urls); > > > } > > > > @Override > > > /** > > > * add ckasspath to the loader. > > > */ > > > public void addURL(URL url) { > > > super.addURL(url); > > > } > > > > } > > > > Then to add my jar file to classpath at runtime I use the following > > > code ( see below). > > > > private void addToClasspath(){ > > > try > > > { > > > URL urls [] = {}; > > > > String path = "/home/xddmid/out/xml.jar"; > > > String urlPath = "jar:file://" + path + "!/"; > > > URL url = new URL (urlPath); > > > ExtendedClassLoader cl = new ExtendedClassLoader (urls); > > > cl.addURL(url); > > > String resource = > > > ClassLoader.getSystemClassLoader().getResource("test.xml").toExternalForm(); > > > System.out.println ("Success! Resource "+resource + " > > > found!"); > > > } > > > catch (Exception ex) > > > { > > > System.out.println ("Failed."); > > > ex.printStackTrace (); > > > } > > > > I have verified that the jarfile exists and that it contains "test.xml > > > file" ( "jar tvf xml.jar). > > > The "test.xml" file is directly in root ( no package name). > > > > I get a NullPointerException for this line: > > > > String resource = > > > ClassLoader.getSystemClassLoader().getResource("test.xml").toExternalForm(); > > > Why are you using the system classloader and not the one you wrote? In > > other words, that should be cl.getResource... > > > Alessio > > This line is used in code to access system resources. I cannot change > it. > > ClassLoader.getSystemClassLoader().getResource("test.xml").toExternalForm(); Then you cannot dynamically add jars to the classpath. Problem solved. ;) Alessio
From: Alessio Stalla on 20 Apr 2010 16:57
On 20 Apr, 20:56, Lothar Kimmeringer <news200...(a)kimmeringer.de> wrote: > Then you need to replace the system class loader with your > ClassLoader. Is it possible? - Alessio |