简体   繁体   English

如何在Java运行时重载类

[英]How to overload class at runtime in Java

It's possible to add modified classes to a jar file manually, but to make it simple I want to do it on runtime. 可以将修改后的类手动添加到jar文件中,但是为了简单起见,我想在运行时进行操作。

Lets put it this way: 让我们这样说:

JAR_A.jar - contains A.class and B.class JAR_A.jar-包含A.class和B.class

JAR_B.jar - contains modified version of B.class JAR_B.jar-包含B.class的修改版本

Instead of adding the modified B.class manually with an archive manager I want to accomplish the same on runtime. 与其使用存档管理器手动添加修改后的B.class,我想在运行时完成相同的操作。

http://www.java2s.com/Code/Java/File-Input-Output/CreateJarfile.htm http://www.java2s.com/Code/Java/File-Input-Output/CreateJarfile.htm

 FileOutputStream fout = new FileOutputStream("c:/tmp/foo.jar");
 JarOutputStream jarOut = new JarOutputStream(fout);
 jarOut.putNextEntry(new ZipEntry("com/foo/")); // Folders must end with "/".
 jarOut.putNextEntry(new ZipEntry("com/foo/Foo.class"));
 jarOut.write(getBytes("com/foo/Foo.class"));
 jarOut.closeEntry();
 jarOut.putNextEntry(new ZipEntry("com/foo/Bar.class"));
 jarOut.write(getBytes("com/foo/Bar.class"));
 jarOut.closeEntry();
 jarOut.close();
 fout.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM