How can identify Applet subclasses from a Jar file

2019-11-02 17:37发布

我在jar文件两个类层次结构,其中唯一的区别是, Main在example2.jar没有包,而Main在example1.jar是包的一部分user

example1.jar

// user/Main.java
package user;
import Engine.GameWindow;
public class Main extends GameWindow { }

// Engine/GameWindow.java
package Engine;
import java.applet.Applet;
public abstract class GameWindow extends Applet { }

example2.jar

// Main.java
import Engine.GameWindow;
public class Main extends GameWindow { }

// Engine/GameWindow.java
package Engine;
import java.applet.Applet;
public abstract class GameWindow extends Applet { }

我创造了这个程序,以确定哪个jar文件里面类的Applet的子类。

import java.applet.Applet;
public class JarClassIdentifier 
{
  public static void main(String[] args) 
    throws Exception 
  {
    // example1.jar: args = { "user.Main.class", "Engine.GameWindow.class" }
    // example2.jar: args = { "Main.class", "Engine.GameWindow.class" }
    for(String class_name : args) {
      class_name = class_name.replace(".class", "");

      Class<?> c = Class.forName(class_name);

      if(Applet.class.isAssignableFrom(c))
        System.out.println(class_name);
    }
  }
}   

对于example1.jar,程序正确打印user.MainEngine.GameWindow 。 为什么example2.jar只打印出Engine.GameWindow

文章来源: How can identify Applet subclasses from a Jar file?