Question

I am trying to port my LWJGL Application to an Applet to be played online, however, I can not figure out how to give the correct path to the natives.

Here is the .java Applet code:

package net.foxycorndog.idk.applet;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Canvas;

import net.foxycorndog.idk.Idk;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;

public class IdkApplet extends Applet
{
    Canvas  drawCanvas;

    Idk     idk;

    /** is the game loop running */
    boolean running = false;

    public void startLWJGL()
    {
        idk.start(drawCanvas);
    }

    /**
     * Tell game loop to stop running, after which the LWJGL Display will be
     * destroyed. The main thread will wait for the Display.destroy().
     */
    private void stopLWJGL()
    {
        running = false;

        try
        {
            idk.getGameThread().join();
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }

    public void start()
    {

    }

    public void stop()
    {

    }

    /**
     * Applet Destroy method will remove the canvas, before canvas is destroyed
     * it will notify stopLWJGL() to stop the main game loop and to destroy the
     * Display
     */
    public void destroy()
    {
        super.destroy();
        System.exit(0);
    }

    public void init()
    {
        Idk.init();

        idk = new Idk();

        setLayout(new BorderLayout());
        try
        {
            drawCanvas = new Canvas()
            {
                public final void addNotify()
                {
                    super.addNotify();
                    startLWJGL();
                }

                public final void removeNotify()
                {
                    stopLWJGL();
                    super.removeNotify();
                }
            };

            setSize(640, 512);

            drawCanvas.setSize(getWidth(), getHeight());
            add(drawCanvas);
            drawCanvas.setFocusable(true);
            drawCanvas.requestFocus();
            drawCanvas.setIgnoreRepaint(true);
            setVisible(true);
        }
        catch (Exception e)
        {
            System.err.println(e);
            throw new RuntimeException("Unable to create display");
        }
    }

    protected void initGL()
    {

    }
}

Heres the HTML code:

<html>
    <body>
        <center>
            <div class="rounded">
                <applet code="net.foxycorndog.idk.applet.IdkApplet" name="theapplet" archive="Idk.jar" width="640" height="512" codebase="../applets/Idk">
                    <!-- The following tags are mandatory -->

                    <!-- Name of Applet, will be used as name of directory it is saved in, and will uniquely identify it in cache -->
                    <param name="al_title" value="appletloadertest">

                    <!-- Main Applet Class -->
                    <param name="al_main" value="net.foxycorndog.idk.applet.IdkApplet">

                    <!-- List of Jars to add to classpath -->
                    <param name="al_jars" value="lwjgl_applet.jar.pack.lzma, lwjgl.jar.pack.lzma, lwjgl_util.jar.pack.lzma">

                    <!-- signed windows natives jar in a jar --> 
                    <param name="al_windows" value="windows_natives.jar.lzma">

                    <!-- signed linux natives jar in a jar --> 
                    <param name="al_linux" value="linux_natives.jar.lzma">

                    <!-- signed mac osx natives jar in a jar --> 
                    <param name="al_mac" value="macosx_natives.jar.lzma">

                    <!-- signed solaris natives jar in a jar --> 
                    <param name="al_solaris" value="solaris_natives.jar.lzma">

                    <!-- Tags under here are optional -->

                    <!-- whether to use cache - defaults to true -->
                    <!-- <param name="al_cache" value="true"> -->

                    <!-- Version of Applet (case insensitive String), applet files not redownloaded if same version already in cache -->
                    <!-- <param name="al_version" value="0.1"> -->

                    <!-- Specify the minimum JRE version required by your applet, defaults to "1.5" -->
                    <!-- <param name="al_min_jre" value="1.6"> -->

                    <!-- background color to paint with, defaults to white -->
                    <!-- <param name="boxbgcolor" value="#000000"> -->

                    <!-- foreground color to paint with, defaults to black -->
                    <!-- <param name="boxfgcolor" value="#ffffff"> -->

                    <!-- logo to paint while loading, will be centered, defaults to "appletlogo.gif" -->
                    <!-- <param name="al_logo" value="appletlogo.gif"> -->

                    <!-- progressbar to paint while loading. Will be painted on top of logo, width clipped to percentage done, defaults to "appletprogress.gif" -->
                    <!-- <param name="al_progressbar" value="appletprogress.gif"> -->

                    <!-- whether to run in debug mode -->
                    <!-- <param name="al_debug" value="true"> -->

                    <!-- whether to prepend host to cache path - defaults to true -->
                    <!-- <param name="al_prepend_host" value="true"> -->

                    <param name="separate_jvm" value="true">

                    <p>
                        You're browser must have java enabled to view this content. If you do not have jave installed or the newest version, you can click here to update it to the latest version.
                        <a href="http://java.com/en/download/">Java</a>
                    </p>
                </applet>
            </div>
        </center>
    </body>
</html>

Heres the error output:

Exception in thread "Thread-14" java.lang.UnsatisfiedLinkError: no lwjgl in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1758)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1045)
at org.lwjgl.Sys$1.run(Sys.java:73)
at java.security.AccessController.doPrivileged(Native Method)
at org.lwjgl.Sys.doLoadLibrary(Sys.java:66)
at org.lwjgl.Sys.loadLibrary(Sys.java:95)
at org.lwjgl.Sys.<clinit>(Sys.java:112)
at org.lwjgl.opengl.Display.<clinit>(Display.java:132)
at net.foxycorndog.presto2d.PrestoGL2D.createFrame(PrestoGL2D.java:172)
at net.foxycorndog.idk.Frame.init(Frame.java:163)
at net.foxycorndog.idk.Frame.<init>(Frame.java:75)
at net.foxycorndog.idk.Idk$1$1.<init>(Idk.java:118)
at net.foxycorndog.idk.Idk$1.run(Idk.java:118)
Was it helpful?

Solution

There is a significant difference between how native libraries are located by the Java runtime when executing in a desktop app vs in an Applet. Because an Applet gets bundled and transmitted to be run in the clients browser you must include most dependencies that it will need, especially true for native libraries that it references. Luckily, LWJGL provides a helper class for managing applications that are deployed as applets.

Read more about the LWJGL AppletLoader and how to incorporate it into your application.

OTHER TIPS

The head of your applet tag is wrong, is should be as follows

<applet code="org.lwjgl.util.applet.AppletLoader"
    archive="lwjgl_util_applet.jar" 
    codebase="." 
    width="640" height="512">

Also the idk.jar should be added to the al_jars parameter. The rest looks correct.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top