简体   繁体   中英

How can I determine the entry point of Java applet from its JAR file?

Is there a way to determine the starting point of a java applet given just the jar file? I'm trying to create a website where I can upload an applet's jar file and then play it back with something similar to the following.

<object type="application/x-java-applet" height="450" width="450">
  <param name="code" value="<? echo $startingClass; ?>" />
  <param name="archive" value="<? echo $jarUrl; ?>" />
  Applet failed to run.
</object>

The applet's I've been given do not have correct manifest or jnlp files. I've tried using jar -tvf 'jarfile.jar' to get a list of all of the classes in the jar file, but can't determine which one is the entry point.

Any help is appreciated!

Sounds very complicated to me. Roughly I think this is needed:

  1. after upload of the jar on the server, you would need a custom class loader to load the jar on your server (see Load jar dynamically ).
  2. Then you can use Reflection to find applet main class in it,
  3. then you would redirect to a JSP where you can generate the needed <object> tag.

.. but can't determine which one is the entry point.

Use something like:

if (exampleOfClass instanceof Applet) { // We're good to go!...

Here is a partial solution based on the other comments, but I ran into an issue where not all subclasses of applet were being identified ( How can identify Applet subclasses from a Jar file? ). The program will print out the classes inside the Jar file that are a subclass of Applet . One of those classes (possibly the most-derived class) is the entry point.

import java.applet.Applet;

public class JarAppletIdentifier 
{  
  public static void main(String [] args)
    throws Exception
  {
    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);
    }   
  }
}

This snippet expects as arguments the list of classes in the jar file.

Using linux, the arguments can be read from the jar file and parsed using

// prints out, eg. 
//   pac.kage.Main.class
//   pac.kage.Other.class
//   user.pac.kage.SomeClass.class
jar_classes=$(jar -tf example.jar | 
  grep .class | grep -v "\\$" | grep -v "^\." | sed 's/\//\./g')

You can then pass this result into the program with

echo $jar_classes | xargs java -classpath "example.jar" JarAppletIdentifier

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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