主题:Classloader高手进
我在网上找了一段代码,运行没有混淆的代码就可以,但是混淆过后的 会抛出以下的异常,高手指点一下..谢谢了
Exception in thread "main" java.lang.ClassFormatError: Unknown constant tag 0 in class file a
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at emulator.classloader.MidletLoader.loadClass(MidletLoader.java:159)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at emulator.Emulator$1.widgetSelected(Emulator.java:97)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:228)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1003)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3823)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3422)
at emulator.Emulator.open(Emulator.java:56)
at emulator.Emulator.main(Emulator.java:41)
代码如下:
public Class<?> loadClass(String className) throws ClassNotFoundException
{
JarInputStream jarIn = null;
String classPath = className.replace(".", "/")+".class";
try
{
ByteArrayInputStream jarDataIn = new ByteArrayInputStream(this.data, offset, len);
jarIn = new JarInputStream(jarDataIn);
JarEntry entry;
while (null != (entry = jarIn.getNextJarEntry()))
{
if (entry.getName().equals(classPath))
{
break;
}
}
if (null == entry)
{
if (null != superLoader)
{
return superLoader.loadClass(className);
}
else
{
return super.loadClass(className);
}
}
byte[] data = null;
if (-1 == entry.getSize())
{
ByteArrayOutputStream dataOut = new ByteArrayOutputStream();
byte[] packData = new byte[2048];
int readLen = 0;
while (-1 != (readLen = jarIn.read(packData)))
{
dataOut.write(packData, 0, readLen);
}
if (dataOut.size() <= 0)
{
throw new ClassNotFoundException(className);
}
data = dataOut.toByteArray();
}
else
{
data = new byte[(int)entry.getSize()];
if (-1 == jarIn.read(data, 0, data.length))
{
throw new ClassNotFoundException(className);
}
}
Class<?> cs = defineClass(className, data, 0, data.length);
return cs;
}
catch(IOException err)
{
throw new ClassNotFoundException(err.getMessage(), err);
}
finally
{
if (null != jarIn)
{
try
{
jarIn.close();
}
catch(IOException err)
{
err = null;
}
}
}
}
Exception in thread "main" java.lang.ClassFormatError: Unknown constant tag 0 in class file a
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at emulator.classloader.MidletLoader.loadClass(MidletLoader.java:159)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at emulator.Emulator$1.widgetSelected(Emulator.java:97)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:228)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1003)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3823)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3422)
at emulator.Emulator.open(Emulator.java:56)
at emulator.Emulator.main(Emulator.java:41)
代码如下:
public Class<?> loadClass(String className) throws ClassNotFoundException
{
JarInputStream jarIn = null;
String classPath = className.replace(".", "/")+".class";
try
{
ByteArrayInputStream jarDataIn = new ByteArrayInputStream(this.data, offset, len);
jarIn = new JarInputStream(jarDataIn);
JarEntry entry;
while (null != (entry = jarIn.getNextJarEntry()))
{
if (entry.getName().equals(classPath))
{
break;
}
}
if (null == entry)
{
if (null != superLoader)
{
return superLoader.loadClass(className);
}
else
{
return super.loadClass(className);
}
}
byte[] data = null;
if (-1 == entry.getSize())
{
ByteArrayOutputStream dataOut = new ByteArrayOutputStream();
byte[] packData = new byte[2048];
int readLen = 0;
while (-1 != (readLen = jarIn.read(packData)))
{
dataOut.write(packData, 0, readLen);
}
if (dataOut.size() <= 0)
{
throw new ClassNotFoundException(className);
}
data = dataOut.toByteArray();
}
else
{
data = new byte[(int)entry.getSize()];
if (-1 == jarIn.read(data, 0, data.length))
{
throw new ClassNotFoundException(className);
}
}
Class<?> cs = defineClass(className, data, 0, data.length);
return cs;
}
catch(IOException err)
{
throw new ClassNotFoundException(err.getMessage(), err);
}
finally
{
if (null != jarIn)
{
try
{
jarIn.close();
}
catch(IOException err)
{
err = null;
}
}
}
}