add jar to classpath at runtime but not able to access resource injar

M

mike

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
 
A

Alessio Stalla

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
 
M

mike

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
 
A

Alessio Stalla

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

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
 
L

Lothar Kimmeringer

mike said:
This line is used in code to access system resources. I cannot change
it.

Then you need to replace the system class loader with your
ClassLoader.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
M

markspace

Alessio said:
Is it possible?

- Alessio

Get the java.lang.ClassLoader source from the OpenJDK project. Adjust
the source to do what you want and compile it. Add your class into the
classpath with -Xbootclasspath/p
 
L

Lothar Kimmeringer

Alessio said:
Is it possible?

<sarcasm>If there only would be something that allows to enter
terms to search the web for or something like a documentation
of things concerning the Java API. Oh wait!...</sarcasm>

http://www.google.de/#q=replace+system+classloader+java
and
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/ClassLoader.html#getSystemClassLoader()
should be easy to find:

| If the system property "java.system.class.loader" is defined
| when this method is first invoked then the value of that
| property is taken to be the name of a class that will be
| returned as the system class loader. The class is loaded
| using the default system class loader and must define a public
| constructor that takes a single parameter of type ClassLoader
| which is used as the delegation parent. An instance is then
| created using this constructor with the default system class
| loader as the parameter. The resulting class loader is defined
| to be the system class loader.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top