how to change default classloader by a command-line argument to the JVM v6

.....or some other means that are completely external to the Java program (the classloader can be changed at runtime by the program itself, but that's not what I am looking for).

I've heard it could be done for the IBM

JVM, but I'm looking for the exact way for the Sun JVM.

Thanks .

[312 byte] By [towstta] at [2007-11-27 6:16:18]
# 1

Can you change it by setting the system property:

java.system.class.loader

through the -D switch?

The following was extracted from the ClassLoader javadoc under getSystemClassLoader():

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

Navy_Codera at 2007-7-12 17:27:47 > top of Java-index,Java Essentials,Java Programming...
# 2
I am novice to java . Can you provide more specific information ?what is the exact command line I should run ?
towstta at 2007-7-12 17:27:47 > top of Java-index,Java Essentials,Java Programming...
# 3
java -Djava.system.class.loader=[the name of your classloader]Now I have a question for you: since you are a novice to Java, why would you want to change the default class loader?
Navy_Codera at 2007-7-12 17:27:47 > top of Java-index,Java Essentials,Java Programming...
# 4

Well , I am wokring on some project which requires changing classes while they are loaded . I am working with someone who is much better than me at JAVA (he just doesn't know the answer to this specific question), so I guess we know what we are doing (at least he does :)) .

But I am learning fast . Anyway , man , thanks . Haven't checked this yet , will do tommorow . Here go your stars .... 5 now , and 5 after I see it actually works :)))

towstta at 2007-7-12 17:27:47 > top of Java-index,Java Essentials,Java Programming...
# 5

I've tried it , and have discovered an additional problem .

What should be the constructior of my new class loader ?

Here is the code of all classes :

******************************

public class TestClass { }

*****************************

*********************************

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

public class MyClassLoader extends ClassLoader {

public static final String HELLO_WORLD = "TestClass" ;

public MyClassLoader () {

super(ClassLoader.getSystemClassLoader().getParent()) ;

}

public Class findClass(String name) throws ClassNotFoundException {

try {

InputStream in = ClassLoader.getSystemResourceAsStream (

name.replace('.','/') + ".class");

byte[] buffer = new byte[100000] ;

int len = in.read(buffer);

byte[] cf = new byte[len];

System.arraycopy(buffer,0,cf,0,len);

if (name.equals(HELLO_WORLD))

System.out.println("It works");

return defineClass(name,cf,0,cf.length);

} catch (Exception e) {

throw new ClassNotFoundException (HELLO_WORLD) ;

}

}

}

**************************************

****************************************

public class HelloWorld {

public static void main(String[] args) {

TestClass test ;

System.out.println("Hello world") ;

}

}

************************************************

I've tried the following command :

java -Djava.system.class.loader=MyClassLoader HelloWorld

Here is the error i've got :

Error occurred during initialization of VM

java.lang.Error: java.lang.NoSuchMethodException: MyClassLoader.<init>(java.lang

.ClassLoader)

at java.lang.ClassLoader.initSystemClassLoader(Unknown Source)

at java.lang.ClassLoader.getSystemClassLoader(Unknown Source)

Caused by: java.lang.NoSuchMethodException: MyClassLoader.<init>(java.lang.Class

Loader)

at java.lang.Class.getConstructor0(Unknown Source)

at java.lang.Class.getDeclaredConstructor(Unknown Source)

at java.lang.SystemClassLoaderAction.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at java.lang.ClassLoader.initSystemClassLoader(Unknown Source)

at java.lang.ClassLoader.getSystemClassLoader(Unknown Source)

Would the command succeed I would expect the following result :

It works

Hello World

I understand that MyClassLoader constructor is wrong since I am trying to get parent of SystemClassLoader which is now MyClassLoader itself and not the 'old regular' SystemClassLoader , but how can I get it now to properly initialize my class loader ?

Thanks .

towstta at 2007-7-12 17:27:47 > top of Java-index,Java Essentials,Java Programming...
# 6

You need to provide a constructor that takes another ClassLoader as it's argument:

public class MyClassLoader extends ClassLoader

{

public MyClassLoader(ClassLoader classLoader)

{

....

}

....

}

I would suspect that the VM will pass the VM's default class loader to your guy to allow you to decorate their implementation if you wish.

Steve-Sinclaira at 2007-7-12 17:27:47 > top of Java-index,Java Essentials,Java Programming...