2013-08-30 49 views
1

我想從字節流中寫入類加載器,但出現此錯誤。我讀過文檔,我很困惑,因爲這不應該發生。這裏是我試圖負載:Java classloader無法在java.lang.Class的Class上調用newInstance()

class LoadedClass { 
    public void out(String msg) { 
     System.out.println(msg); 
    } 
} 

存儲在一個字節數組

這裏是我的類加載器代碼:

package byteclassloader; 

import java.lang.reflect.InvocationTargetException; 
import java.lang.reflect.Method; 

class PrintClass extends ClassLoader { 
    private String LoadedClass = "cafebabe00000033001e0a0005001309001400150a001600170700180700190100063c696e69743e010003282956010004436f646501000f4c696e654e756d6265725461626c650100124c6f63616c5661726961626c655461626c650100047468697301000d4c4c6f61646564436c6173733b0100036f7574010015284c6a6176612f6c616e672f537472696e673b29560100036d73670100124c6a6176612f6c616e672f537472696e673b01000a536f7572636546696c650100104c6f61646564436c6173732e6a6176610c0006000707001a0c000d001b07001c0c001d000e01000b4c6f61646564436c6173730100106a6176612f6c616e672f4f626a6563740100106a6176612f6c616e672f53797374656d0100154c6a6176612f696f2f5072696e7453747265616d3b0100136a6176612f696f2f5072696e7453747265616d0100077072696e746c6e002000040005000000000002000000060007000100080000002f00010001000000052ab70001b100000002000900000006000100000001000a0000000c000100000005000b000c00000001000d000e00010008000000400002000200000008b200022bb60003b10000000200090000000a00020000000300070004000a00000016000200000008000b000c000000000008000f0010000100010011000000020012"; 
    private String name = "LoadedClass"; 

    public Class findClass() { 
     byte[] ba = hexStringToByteArray(LoadedClass); 
     return defineClass(name, ba, 0, ba.length); 
    } 

    private static byte[] hexStringToByteArray(String s) { 
     int len = s.length(); 
     byte data[] = new byte[len/2]; 

     for(int i = 0; i < len; i+= 2) { 
      data[i/2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) 
            + Character.digit(s.charAt(i+1), 16)); 
     } 

     return data; 
    } 
} 

public class ByteClassLoader { 


    public static void main(String[] args) { 
     try { 
      Class LoadedClass = new PrintClass().findClass().getClass(); 
      Object inst = LoadedClass.newInstance(); 
      String out = "The quick brown fox jumps over the gluttonous sloth sow."; 
      Method printStr = LoadedClass.getMethod("out", new Class[] { String.class }); 
      printStr.invoke(inst, out); 
     } 
     catch(InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e) { 
      System.out.println(e.getMessage()); 
     } 
    } 


} 

它拋出該異常:

對於java.lang.Class類,不能調用newInstance()

+0

傑森 - 我猜你想創建使用類加載器加載的類的實例。對?那麼你應該改變問題的標題。 –

回答

4
Class loadedClass = new PrintClass().findClass(); 

不要做的findClass()getClass()因爲它會返回java.lang.Class

現在得到的類加載可以創建實例後:

Object inst = loadedClass.newInstance(); //if the default constructor is present 
+0

@ ErnestFriedman-Hill爲什麼這是不相關的? IMO OP想要創建使用類加載器加載的類的實例。 –

+1

對不起,我的評論是在錯誤的答案 –

+0

@ ErnestFriedman-Hill沒關係。 –

3

findClass()已經返回一個加載類的Class實例。這就是你應該打電話給newInstance()。否則

new PrintClass().findClass().getClass(); 

回報和Class<Class>類型的實例,因爲Class設有私人空的構造。它無法訪問。

/* 
* Constructor. Only the Java Virtual Machine creates Class 
* objects. 
*/ 
private Class() {} 
相關問題