我在下面的链接中举了一个例子:
http://www.javaworld.com/javaworld/jw-10-1996/jw-10-indepth.html?page=1这是我得到的输出:
此程序将使用SimpleClassLoader。>>>>>>负载类:TestClass>>>>>>不是系统类。>>>>>>获取TestClass的实现文件已成功读取线程"main"java.lang.ClassFormatError中出现异常:类文件中的magic值1768779887不兼容位于java.lang.ClassLoader.defineClass1(本机方法)位于java.lang.ClassLoader.defineClassCond(未知源)位于java.lang.ClassLoader.defineClass(未知源)位于java.lang.ClassLoader.defineClass(未知源)哈哈哈。SimpleClassLoader.loadClass(SimpleClassLoader.java:80)哈哈哈。SimpleClassLoader.loadClass(SimpleClassLoader.java:42)哈哈哈。Example.main(Example.java:11)
下面是我读取文件的方法的一个例子:
private byte getClassImplFromDataBase(String className)[] {
System.out.println(" >>>>>> Fetching the implementation of "+className);
byte result[];
try {
FileInputStream fi = new FileInputStream("C:/delete/"+className+".impl");
result = new byte[fi.available()];
fi.read(result);
System.out.println("file was read succesfullyl");
return result;
} catch (Exception e) {
/*
* If we caught an exception, either the class wasnt found or it
* was unreadable by our process.
*/
return null;
}
}
我将TestClass.impl文件放在C:\/delete目录下。我的课程位于包下:啊哈因此,文件TestClass.impl读取正常,但在result=defineClass(classData,0,classData.length);我知道这种方法已被弃用,但仍然??
我不知道.impl
是什么文件类型,但我怀疑它不是一个包含字节码的类文件。十六进制中的幻数1768779887
是ASCII中的696D706F
或impo
。(我怀疑import
的前四个字节)
因此,在我看来,你试图加载未编译的Java代码,而这是你无法做到的(这就是编译器的作用)你最好使用编译器API,但我怀疑你真的需要像OSGi这样的东西,因为试图自己加载类需要你对类加载的工作方式有非常详细的了解。
我在一个常规的struts2项目中也遇到过类似的错误,解决了清理项目的问题。
8266 ERROR [main]: Excepción arrancando filtro struts2
java.lang.ClassFormatError: Incompatible magic value 0 in class file com/lm/action/musician/MusicianList
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1650)
at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:856)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1305)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1187)
at com.opensymphony.xwork2.util.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:144)
at com.opensymphony.xwork2.ObjectFactory.getClassInstance(ObjectFactory.java:108)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyAction(XmlConfigurationProvider.java:424)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider.java:378)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:495)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:286)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:112)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:234)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:390)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:437)
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:74)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:225)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:308)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:79)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3563)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4133)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1012)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1012)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:442)
at org.apache.catalina.core.StandardService.start(StandardService.java:450)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:680)
at org.apache.catalina.startup.Catalina.start(Catalina.java:536)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:275)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
8266 ERROR [main]: Excepción arrancando filtro struts2
java.lang.ClassFormatError: Incompatible magic value 0 in class file com/lm/action/musician/MusicianList
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1650)
at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:856)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1305)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1187)
at com.opensymphony.xwork2.util.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:144)
at com.opensymphony.xwork2.ObjectFactory.getClassInstance(ObjectFactory.java:108)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyAction(XmlConfigurationProvider.java:424)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider.java:378)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:495)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:286)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:112)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:234)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:390)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:437)
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:74)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:225)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:308)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:79)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3563)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4133)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1012)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1012)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:442)
at org.apache.catalina.core.StandardService.start(StandardService.java:450)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:680)
at org.apache.catalina.startup.Catalina.start(Catalina.java:536)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:275)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
abr 19, 2013 10:32:25 AM org.apache.catalina.core.StandardContext start
SEVERE: Error filterStart
abr 19, 2013 10:32:25 AM org.apache.catalina.core.StandardContext start