lotus

贵有恒何必三更眠五更起 最无益只怕一日曝十日寒

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  1846 随笔 :: 0 文章 :: 109 评论 :: 288万 阅读

 

在精简JRE过程中,将rt.jar中类通过FileInputStream,FileOutputStream进行拷贝操作出错:

java.lang.ClassFormatError: Extra bytes at the end of class file

 

源代码:

Java代码  收藏代码
  1. byte buf[] = new byte[256];  
  2. while(fin.read(buf) != -1)  
  3. {  
  4.     fout.write(buf);  
  5.     ir.read();  
  6. }  
  7. fout.flush();  

 

修改后:

Java代码  收藏代码
  1. byte buf[] = new byte[256];  
  2. int len = 256;  
  3. while((len = fin.read(buf)) != -1)  
  4. {  
  5.     fout.write(buf,0,len);  
  6.     ir.read();  
  7. }  
  8. fout.flush();  

 

 

相关资料:

java.lang.ClassFormatError thrown when applet runs


Symptoms

When running an applet in a browser using the Sun JVM, a ClassFormatError is thrown by theClassLoader. The same applet runs under the Microsoft  VM.

Cause

This error is caused by bytecodes generated from old JDK 1.0.2/1.1 compilers, or from a third-party obfuscator. In the past, many of these compilers and obfuscators generated bytecode that does not conform to the Java VM Specification. Because the verifiers in recent J2SE releases are much stricter about bad class format, the ClassFormatError is thrown by the VM when these bad class files are loaded.

Some typical problems in some older class files are the following (note that this list is not exhaustive):

  • There are extra bytes at the end of the class file.
  • The class file contains method or field names that do not begin with a letter.
  • The class attempts to access private members of another class.
  • The class file has other format errors, including illegal constant pool indices and illegal UTF-8 strings.
  • The class file produced by an early (third-party) bytecode obfuscator violates proper class-file format.

Resolution

To allow some of the applets with bad class files to run in the Java 2 platform, Java Plug-in contains a bytecode transformer to transform some of the bad class files to good ones. Currently, only bad class files with the following problems may be transformed:

  • Local variable name with a bad constant pool index
  • Extra bytes at the end of the class file
  • Code segment of the wrong length
  • Illegal field/method name
  • Illegal field/method modifiers
  • Invalid start_pc/length in local var table

Unfortunately, the bytecode transformer cannot transform the following problems, which will still result in a ClassFormatError:

  • Illegal use of nonvirtual function call
  • Arguments can't fit into locals
  • Unsorted lookup switch
  • Truncated class file

You can resolve these problems by simply recompiling your Java classes with the javac compiler from the Java 2 SDK. If you choose to use a third-party obfuscator, be sure to use one that produces class files that respect proper class-file format.

相关资料: http://download.oracle.com/javase/1.4.2/docs/guide/deployment/deployment-guide/upgrade-guide/article-01.html

posted on   白露~  阅读(4999)  评论(0编辑  收藏  举报
编辑推荐:
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示