Question

For reference, I previously asked a question about an error with essentially converting my Java application to an applet. Well, I was suggested to try JavaWebStart, and I am still receiving problems with this way as well, so I decided to create a new question.

Here is the question I'm referencing: java.lang.reflect.invocationtargetexception error in applet

I'm assuming I am assuming I have some kind of structure wrong, of how to setup a JavaWebStart application, as I have tested my code locally as a jar file and had no errors running it.

Here is an example page: http://fogest.com/java_example/

Était-ce utile?

La solution

It seems your code does have a main(String[] args) as we were discussing in your last question. As such, it might be as simple as changing:

<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="http://fogest.com/java_example" href="">
    <information>
        <title>Launch applet with Web Start</title>
        <vendor>Foo Bar Inc.</vendor>
        <offline-allowed/>
    </information>
    <resources>
        <j2se version="1.5+" href="http://java.sun.com/products/autodl/j2se"/>
        <jar href="physics.jar" main="true" />
    </resources>
    <applet-desc
         name="Physics" main-class="main.MainGame"
         width="300" height="200">
    </applet-desc>
  <update check="background"/>
</jnlp>

To something like:

<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="http://fogest.com/java_example" href="">
    <information>
        <title>Launch applet with Web Start</title>
        <vendor>Foo Bar Inc.</vendor>
        <offline-allowed/>
    </information>
    <resources>
        <j2se version="1.5+" href="http://java.sun.com/products/autodl/j2se"/>
        <jar href="physics.jar" main="true" />
    </resources>
    <application-desc main-class="main.MainGame">
    </application-desc>
  <update check="background"/>
</jnlp>

Note

  1. I did not validate either JNLP, but you should. I wrote JaNeLA for doing exactly that.
  2. Swing GUIs should be created & updated on the EDT. See Concurrency in Swing (especially the part on 'initial threads') for more details.
  3. This is an SSCCE based on the frame.

import java.awt.*;
import javax.swing.*;

public class MainGame {
    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;

    public MainGame() {
        run();
    }

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


        JPanel options = new JPanel();
        options.add(new JLabel("Options"));
        JPanel game = new JPanel();
        game.add(new JLabel("Game"));

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

    public static void main(String[] args) {
        new MainGame();
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top