【问题标题】:How to overload class at runtime in JavaJava如何在运行时重载类
【发布时间】:2012-08-05 22:13:44
【问题描述】:

可以手动将修改后的类添加到 jar 文件中,但为了简单起见,我想在运行时进行。

让我们这样说:

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

JAR_B.jar - 包含 B.class 的修改版本

我不想使用存档管理器手动添加修改后的 B.class,而是希望在运行时完成相同的操作。

【问题讨论】:

    标签: java class runtime overloading


    【解决方案1】:

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-04
      • 1970-01-01
      • 2012-04-11
      • 1970-01-01
      • 2012-06-10
      • 1970-01-01
      • 1970-01-01
      • 2012-03-31
      相关资源
      最近更新 更多