Results 1 to 9 of 9

Thread: classloader

  1. #1
    vijayabaskar is offline Member
    Join Date
    Apr 2009
    Posts
    37
    Rep Power
    0

    Thumbs up classloader

    Hey i have create servlet container using osgi framework. i have multiple war files and then i extract it.. after extract the war it has multiple class files.. i have doubt on how to get multiple war files in main function and the how to get class files using classloader.. i hope you will help me to develope my project..

    classByte = loadClassData(classPath);
    System.out.println("classpath"+classByte.length );
    result = defineClass(className,classByte,0,classByte.length );
    System.out.println("classes"+classes);
    classes.put(className,result);
    System.out.println("return"+result);
    return result;

    but the result only return null value.. i need to get the classname in claspath.. so help to find..

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by vijayabaskar View Post
    i need to get the classname in claspath.. so help to find..
    Can you explain a bit more what you want to do here?

  3. #3
    vijayabaskar is offline Member
    Join Date
    Apr 2009
    Posts
    37
    Rep Power
    0

    Default

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;

    //import sun.awt.image.ByteInterleavedRaster;
    //import sun.misc.URLClassPath;

    /**
    * A custom class loader that loads servlets on the fly
    *
    * @author Adarsh
    * @author Sivaraman
    */
    public class CustomClassLoader extends ClassLoader {


    /**
    * classPath contains the CLASSPATH system variable
    */
    private String classPath;

    /**
    * main() method is used to Test this class loader as a standalone application
    */

    // public static void main(String[] args) {

    // CustomClassLoader cl = new CustomClassLoader();
    // //Class cls = cl.getClass("/home/adarsh/sample/WEB-INF/classes/mypackage/Hello.class", "mypackage.Hello");
    // Class cls = cl.getClass("D:\\QRD\\deployer\\wars\\sample\\WEB-INF\\classes\\mypackage\\Hello.class", "mypackage.Hello");
    // try {
    // HttpServlet hs = (HttpServlet) cls.newInstance();
    // } catch (InstantiationException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // } catch (IllegalAccessException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    // }

    /**
    * Returns the class requested for
    *
    * @param classPath The classPath system variable
    * @param className The className is the complete class name the user requested for
    */

    public Class getClass(String classPath, String className) {
    this.classPath = classPath;
    return loadClass(className, true);
    }

    /**
    * Returns the class bytes
    */

    private byte[] getBytes() throws IOException {
    File f = new File(classPath);
    long len = f.length();
    byte raw[] = new byte[(int) len];
    FileInputStream fis = new FileInputStream(f);
    int r = fis.read(raw);
    if (r != len)
    System.out.println("cant read");
    fis.close();
    return raw;

    }

    /**
    * (non-Javadoc)
    *
    * @param name The complete class name
    * @param resolve boolean value indicating whether the class should be resolved
    * @see ClassLoader#loadClass(String, boolean)
    */

    protected Class loadClass(String name, boolean resolve) {
    System.out.println(name);
    Class clazz = null;
    try {
    // Load the class from the JVM
    clazz = CustomClassLoader.class.getClassLoader().loadClass (name);
    return clazz;
    } catch (ClassNotFoundException e) {
    System.out.println(name + " Not Found in current directory");
    }
    byte raw[] = null;
    try {
    // Get the raw bytes of the class
    raw = getBytes();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


    clazz = defineClass(name, raw, 0, raw.length);

    return clazz;
    }

    }




    CustomClassLoader cc = new CustomClassLoader();
    contexts[0][1]=classpath
    contexts[0][2]=org.qrd.openmayyam.helloworld
    Class cls = cc.getClass(contexts[j][1], contexts[j][2]);

    i need to get only the class name that means hellowold..
    now am getting only null
    please help to fine

  4. #4
    vijayabaskar is offline Member
    Join Date
    Apr 2009
    Posts
    37
    Rep Power
    0

    Default

    try {
    // Load the class from the JVM
    clazz = CustomClassLoader.class.getClassLoader().loadClass (name);
    return clazz;
    } catch (ClassNotFoundException e) {
    System.out.println(name + " Not Found in current directory");
    }


    this only throwing exception

  5. #5
    vijayabaskar is offline Member
    Join Date
    Apr 2009
    Posts
    37
    Rep Power
    0

    Default

    do u have any idea

  6. #6
    vijayabaskar is offline Member
    Join Date
    Apr 2009
    Posts
    37
    Rep Power
    0

    Default

    how to class name form class path using classloader

  7. #7
    vijayabaskar is offline Member
    Join Date
    Apr 2009
    Posts
    37
    Rep Power
    0

    Default

    how to get class name form class path using classloader

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Did you read the Java API for classloader? I'm mess-up what you exactly want to do is. Load the class on last code segment(in try-catch block) And now something different.

    ClassLoader (Java 2 Platform SE v1.4.2)

  9. #9
    vijayabaskar is offline Member
    Join Date
    Apr 2009
    Posts
    37
    Rep Power
    0

    Default

    hi eranga.. i read it.. but now i have to put the list of servlet class files in hashtable and get from hashtable.. can u sent me the example program for it..

Similar Threads

  1. Replies: 13
    Last Post: 03-16-2009, 09:37 PM
  2. ClassLoader and JVM
    By Pradeen in forum New To Java
    Replies: 0
    Last Post: 01-19-2009, 06:04 PM
  3. Problem of Classloader in Eclipse Plugin Developemt.
    By Ashish Naidu in forum Eclipse
    Replies: 0
    Last Post: 09-12-2008, 10:16 AM
  4. My own ClassLoader didn't work.
    By snooze-g in forum Advanced Java
    Replies: 1
    Last Post: 07-17-2007, 11:12 AM
  5. eclipse classloader problem
    By sandor in forum Eclipse
    Replies: 2
    Last Post: 05-10-2007, 03:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •