Results 1 to 9 of 9
Thread: classloader
- 04-02-2009, 07:36 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 37
- Rep Power
- 0
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..
- 04-02-2009, 08:15 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 04-03-2009, 08:59 AM #3
Member
- Join Date
- Apr 2009
- Posts
- 37
- Rep Power
- 0
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
- 04-03-2009, 09:00 AM #4
Member
- Join Date
- Apr 2009
- Posts
- 37
- Rep Power
- 0
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
- 04-04-2009, 07:50 AM #5
Member
- Join Date
- Apr 2009
- Posts
- 37
- Rep Power
- 0
do u have any idea
- 04-04-2009, 11:42 AM #6
Member
- Join Date
- Apr 2009
- Posts
- 37
- Rep Power
- 0
how to class name form class path using classloader
- 04-04-2009, 11:42 AM #7
Member
- Join Date
- Apr 2009
- Posts
- 37
- Rep Power
- 0
how to get class name form class path using classloader
- 04-06-2009, 04:00 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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)
- 04-06-2009, 08:08 AM #9
Member
- Join Date
- Apr 2009
- Posts
- 37
- Rep Power
- 0
Similar Threads
-
java.sql.SQLException: No suitable driver .... when using Custom ClassLoader
By tomer1960 in forum Advanced JavaReplies: 13Last Post: 03-16-2009, 09:37 PM -
ClassLoader and JVM
By Pradeen in forum New To JavaReplies: 0Last Post: 01-19-2009, 06:04 PM -
Problem of Classloader in Eclipse Plugin Developemt.
By Ashish Naidu in forum EclipseReplies: 0Last Post: 09-12-2008, 10:16 AM -
My own ClassLoader didn't work.
By snooze-g in forum Advanced JavaReplies: 1Last Post: 07-17-2007, 11:12 AM -
eclipse classloader problem
By sandor in forum EclipseReplies: 2Last Post: 05-10-2007, 03:26 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks