0

I have the two following class hierarchy in jar files where the only difference is that Main in example2.jar has no package while Main in example1.jar is part of the package 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 { }

I created this program to determine which classes inside of a jar file are subclasses of 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);
    }
  }
}   

For example1.jar, the program correctly prints user.Main and Engine.GameWindow. Why does example2.jar only print out Engine.GameWindow?

pLiKT
  • 120
  • 1
  • 1
  • 9
  • 2
    *"Main class is part of the package user"* - Then in that case, shouldn't "Main.class" be "user.Main.class"? – MadProgrammer Dec 14 '13 at 04:15
  • Please show your actual code around `Class.forName()`. I suspect you're ignoring a useful `ClassNotFoundException`, either because of the package issue @MadProgrammer noted or because you incorrectly capitalized `.class`. – chrylis -cautiouslyoptimistic- Dec 14 '13 at 04:16
  • I updated my question to fix some typos and clarify what I meant. `args` is the the main function arguments and is being provided by parsing the output of `tar -tf example.jar` – pLiKT Dec 14 '13 at 04:43

0 Answers0