0

My application runs without errors as an application, but as an applet throws the following error:

java.lang.reflect.invocationtargetexception

This is my first time attempting to use my application as an applet, so I may have done something wrong, but here is my main class:

 package main;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JApplet;
import javax.swing.JFrame;

public class MainGame extends JApplet {
    private static final long serialVersionUID = 1L;
    public static final String NAME = "Physics - Projectile Motion Example";
    public static final int HEIGHT = 160;
    public static final int WIDTH = HEIGHT * 16 / 9;
    public static final int SCALE = 4;

    private long reportedFramerate;
    long framerate = 1000 / 60;
    // time the frame began
    long frameStart;
    // number of frames counted this second
    long frameCount = 0;
    // time elapsed during one frame
    long elapsedTime;
    // accumulates elapsed time over multiple frames
    long totalElapsedTime = 0;
    // the actual calculated framerate reported


    public MainGame() {
        run();
    }

    public void run() {
        JFrame frame = new JFrame(MainGame.NAME);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());


        OptionsPanel options = new OptionsPanel();
        GamePanel game = new GamePanel(options);

        frame.setSize(new Dimension ( WIDTH * SCALE, HEIGHT * SCALE ));

        frame.add(game, BorderLayout.CENTER);
        frame.add(options, BorderLayout.SOUTH);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);
        while(true) {
            frameStart = System.currentTimeMillis();

            if(options.isStartGame() == true) {
                game.run();
            }
            else {
                game.reset();
            }

            // calculate the time it took to render the frame
            elapsedTime = System.currentTimeMillis() - frameStart;
            // sync the framerate
            try {
                // make sure framerate milliseconds have passed this frame
                if (elapsedTime < framerate) {
                    Thread.sleep(framerate - elapsedTime);
                } else {
                    // don't starve the garbage collector
                    Thread.sleep(5);
                }
            } catch (InterruptedException e) {
                break;
            }
            ++frameCount;
            totalElapsedTime += (System.currentTimeMillis() - frameStart);
            if (totalElapsedTime > 1000) {
                reportedFramerate = (long) ((double) frameCount
                        / (double) totalElapsedTime * 1000.0);
                // show the framerate in the applet status window
                //System.out.println("fps: " + reportedFramerate);
                // repaint();
                frameCount = 0;
                totalElapsedTime = 0;

                //System.out.println(reportedFramerate);
            }
        }
    }

    public void init() {
        new MainGame();
    }
    public void start() {
        System.out.println("started");
    }
    public void stop() {
        System.out.println("Stopped");
    }
    public void destroy() {

    }


    public static void main(String[] args) {
        new MainGame();
    }

}

HTML for the object:

<p>
<object type="application/x-java-applet"
    name="physics" width="360" height="320">
    <param name="code" value="main.MainGame.class" />
    <param name="archive" value="physics.jar" />
    <param name="scriptable" value="true" />
    <param name="mayscript" value="true" />
    <param name="file" value="/report_files/1-1272041330710YAIwK" />
</object>
</p>

Example page to see the error for yourself: http://fogest.com/java_example/

ComputerLocus
  • 3,448
  • 10
  • 47
  • 96
  • 1
    Can you post the entire exception report? – Hovercraft Full Of Eels Jun 16 '13 at 15:56
  • Also have you gone through the applet tutorials yet? Your code looks a bit unusual to me in that you're not really creating a viewable applet at all with it but rather trying to shoehorn your JFrame into it. My "answer" to your question is basically this: read the applet tutorials and then restructure your code accordingly. – Hovercraft Full Of Eels Jun 16 '13 at 15:57
  • @HovercraftFullOfEels This is all it gives me http://i.imgur.com/jP0kVgP.png . When I hit details it just brings up a Java Console with no information about the error. – ComputerLocus Jun 16 '13 at 16:00
  • @HovercraftFullOfEels Here is an example page. http://fogest.com/java_example/ . The physics.jar file is stored in the same location as the index.html page is. – ComputerLocus Jun 16 '13 at 16:04
  • 1
    Again, your code doesn't look like valid applet code to me. Instead it looks like a JFrame based GUI that has been given minor changes to try to shoehorn it into an applet. Consider re-writing the code as an applet. Also, `reflect.invocationtargetexception` unfortunately doesn't inform us as to what the problem may be. – Hovercraft Full Of Eels Jun 16 '13 at 16:11

1 Answers1

2

The full stack trace is:

Java Plug-in 10.21.2.11
Using JRE version 1.7.0_21-b11 Java HotSpot(TM) Client VM
User home directory = C:\Users\Andrew
----------------------------------------------------
c:   clear console window
...
0-5: set trace level to <n>
----------------------------------------------------
Trace level set to 5: all ... completed.security: blacklist: hasBeenModifiedSince 1370282598700 (we have 1366432497929)
security: blacklist: hasBeenModifiedSince 1366432497958 (we have 1366432497929)
network: Created version ID: 1.7.0.21
network: Created version ID: 1.7.0.21
basic: exception: java.lang.reflect.InvocationTargetException.
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.runOnEDTAndWait(Unknown Source)
    at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.instantiateApplet(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.initAppletAdapter(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
    at com.sun.deploy.uitoolkit.impl.awt.OldPluginAWTUtil.invokeAndWait(Unknown Source)
    ... 5 more
Caused by: java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "exitVM.0")
    at java.security.AccessControlContext.checkPermission(Unknown Source)
    at java.security.AccessController.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkPermission(Unknown Source)
    at sun.plugin2.applet.AWTAppletSecurityManager.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkExit(Unknown Source)
    at javax.swing.JFrame.setDefaultCloseOperation(Unknown Source)
    at main.MainGame.run(MainGame.java:36)
    at main.MainGame.<init>(MainGame.java:31)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter$1.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
basic: Removed progress listener: sun.plugin.util.ProgressMonitorAdapter@1f312f0
security: Reset deny session certificate store
security: blacklist: hasBeenModifiedSince 1366740392097 (we have 1366432497929)
security: blacklist: hasBeenModifiedSince 1369035157319 (we have 1366432497929)
network: CleanupThread used 6369043 us
basic: PluginMain.unregisterApplet: 1 from mananger sun.plugin2.applet.Applet2Manager@1823290
network: Checking for update at: https://javadl-esd-secure.oracle.com/update/baseline.version
network: Checking for update at: https://javadl-esd-secure.oracle.com/update/blacklisted.certs
network: Checking for update at: https://javadl-esd-secure.oracle.com/update/blacklist
security: JSS is not configured
network: Connecting https://javadl-esd-secure.oracle.com/update/baseline.version with proxy=DIRECT
network: Connecting https://javadl-esd-secure.oracle.com/update/blacklisted.certs with proxy=DIRECT
network: Connecting https://javadl-esd-secure.oracle.com/update/blacklist with proxy=DIRECT
network: Connecting http://javadl-esd-secure.oracle.com:443/ with proxy=DIRECT
network: Connecting http://javadl-esd-secure.oracle.com:443/ with proxy=DIRECT
network: Connecting http://javadl-esd-secure.oracle.com:443/ with proxy=DIRECT
security: Loading Root CA certificates from C:\Program Files (x86)\Java\jre7\lib\security\cacerts
security: Loaded Root CA certificates from C:\Program Files (x86)\Java\jre7\lib\security\cacerts
security: Loading SSL Root CA certificates from C:\Program Files (x86)\Java\jre7\lib\security\cacerts
security: Loaded SSL Root CA certificates from C:\Program Files (x86)\Java\jre7\lib\security\cacerts
security: Loading certificates from Deployment session certificate store
security: Loaded certificates from Deployment session certificate store
security: Loading certificates from Deployment session certificate store
security: Loaded certificates from Deployment session certificate store
security: Loading certificates from Deployment session certificate store
security: Loaded certificates from Deployment session certificate store
network: Cookie service is not available - use cache to determine "Cookie"
network: Cookie service is not available - use cache to determine "Cookie"
network: Cookie service is not available - use cache to determine "Cookie"

The important part is:

Caused by: java.security.AccessControlException: access denied 
  ("java.lang.RuntimePermission" "exitVM.0")

That is caused by:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Not even a trusted applet can (or at least should even try) to exit the JVM.

Agree with the general thrust of Hovercraft Full Of Eels in that this is a frame poorly shoe-horned into being an applet. Launch the frame using Java Web Start.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I will try removing some things relating to the frame, but I'm wondering how you retrieved the stacktrace from the browser? – ComputerLocus Jun 16 '13 at 16:17
  • `Trace level set to 5: all` (look for it in the above output). Ensure the [Java Console](http://www.java.com/en/download/help/javaconsole.xml) is configured to show for applets & JWS apps. *If there is no output at the default level, raise it and try again.* – Andrew Thompson Jun 16 '13 at 16:18
  • I guess I will attempt using Java Web Start as you suggested. – ComputerLocus Jun 16 '13 at 16:27
  • Excellent choice. :) – Andrew Thompson Jun 16 '13 at 16:28
  • Hey, I know I accepted this answer, but I am getting the same error message when using Java Web Start (http://fogest.com/java_example/) – ComputerLocus Jun 16 '13 at 16:45
  • That is probably because that JNLP is attempting to launch the original (applet) code. A free floating applet should logically be allowed to end the VM, but I guess it got caught up in the current security manager being a little over cautious. I meant to code it as a `JFrame` with a `main(String[] args)`. – Andrew Thompson Jun 16 '13 at 17:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31831/discussion-between-fogest-and-andrew-thompson) – ComputerLocus Jun 16 '13 at 17:48
  • "continue this discussion in chat" No thanks. I don't want to chat even when on FB. Continue in comments or ask a new question. – Andrew Thompson Jun 16 '13 at 18:39
  • " I've removed the JApplet related code, and only have this to start it: public static void main(String[] args) { new MainGame(); } " – ComputerLocus Jun 16 '13 at 18:47
  • Still the same issue. – ComputerLocus Jun 16 '13 at 18:47