Hot questions for Using Applets in deployment

Question:

So I have the following code in Java which deploys an applet into a html page.

Due to security problems I'm trying to make it a .jar and then sign that .jar to see first how that will work.

Unfortunately I'm not really sure what I should change in order to run it as a .jar, given that the main class in the .jar is the same:

p.println(document+"<applet name=\"myApp\" codebase="+codebase+" code="+code+ " width='+(scnWid-30)+' height='+(scnHei-45)+'>');");
        p.println(document+"<param name=user value=\""+user+"\">');");
        p.println(document+"<param name=sessionid value=\""+sessionid+"\">');");

Where codebase and code are taken here :

public void init(ServletConfig config) throws ServletException {
            super.init(config);
            ServletContext context = getServletContext();
            applet_code_base = context.getInitParameter("applet_code_base");
            applet_code = context.getInitParameter("applet_code");

          }

Answer:

Reference Deploying An Applet In Under 10 Minutes :

  1. Compile / build your applet's Java code and make sure all class files and resources such as images etc. are in a separate directory, example build/components.

  2. Create a jar file containing your applet's class files and resources.

    cd build

    jar cvf DynamicTreeDemo.jar components

  3. Sign your jar file if the applet needs special security permissions, for example, to be launched in a modern JRE with default settings. By default, unsigned code will be blocked.

    jarsigner -keystore myKeyStore -storepass abc123 -keypass abc123 DynamicTreeDemo.jar johndoe

    where keystore is setup and located at "myKeyStore" alias is "johndoe" keystore password and alias password are "abc123"

  4. Create a JNLP file that describes how your applet should be launched.

dynamictree-applet.jnlp

<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="" href="">
    <information>
        <title>Dynamic Tree Demo</title>
        <vendor>Dynamic Team</vendor>
    </information>
    <resources>
        <!-- Application Resources -->
        <j2se version="1.6+"
              href="http://java.sun.com/products/autodl/j2se"
              max-heap-size="128m" />

        <jar href="DynamicTreeDemo.jar" main="true" />

    </resources>
    <applet-desc 
         name="Dynamic Tree Demo Applet"
         main-class="components.DynamicTreeApplet"
         width="300"
         height="300">
     </applet-desc>
</jnlp>     
  1. Create the HTML page that will display the applet. Invoke the runApplet function from the Deployment Toolkit to deploy the applet.

AppletPage.html

<body>
    ....
    <script src="http://java.com/js/deployJava.js"></script>

    <script> 
        var attributes = { code:'components.DynamicTreeApplet',  width:300, height:300} ; 
        var parameters = {jnlp_href: 'dynamictree-applet.jnlp'} ; 
        deployJava.runApplet(attributes, parameters, '1.6'); 
    </script>
    ....
</body>                 
  1. For this example, place DynamicTreeDemo.jar, dynamictree-applet.jnlp, and AppletPage.html in the same directory on the local machine or a web server. A web server is not required for testing this applet.

  2. View `AppletPage.html in a web browser. The Dynamic Tree Demo Applet will be displayed. View Java Console Log for error and debug messages.

For more information see Deployment Toolkit 101 - Java Tutorials Blog

Question:

Is it possible to embed linux shared libs (.so) in Java applets?

I know that I can call JNI code from applets (privileged only), but is it possible embed these libs inside the applet?


Answer:

..is it possible embed these libs inside the applet?

No. Natives should be inside the root path of a separate Jar. There should be separate Jars for OS X, *nix and Windows. Then use Java Web Start to deploy the lot and reference each Jar containing natives in an OS specific resource section of the JNLP launch file - so each OS only downloads the type of natives that it requires.

Question:

So I have a pretty basic Java app I have been writing in Eclipse. I now have realized somewhat late that I want this to run as an applet on my website.

How do I setup JApplet? (assuming this is what I need to use) Is there any "quick fixes" for assuring that the app will be able to be used as an applet?


Answer:

This should get you started http://www.dreamincode.net/forums/topic/28410-application-to-japplet-and-reverse/

It was the first item in a Google Search, so next time look there first.

Question:

I've found exactly the same question I just made but it is 7 years old; so I'd like to have an "updated" answer if it is possible. Thanks.


Answer:

Old days

As commented by TrogDor, there were previously two ways to deploy a Swing app through the web:

  • Java Applet technologyYour app would appear within a rectangle on the web page, within the browser.
  • Java Web Start technologyClicking a link on a web page would download a copy of your Swing app to the user’s local machine, where your app would then be launched locally using a locally-installed JVM. So your app runs separate from the web browser. This click-to-download-and-run process is defined by Java Network Launching Protocol (JNLP).

Both of these are being phased out. For details, see the white paper Java Client Roadmap Update published by Oracle 2018-03.

Nowadays

The modern approach is to build a Swing and/or JavaFX app, then deploy by using a packaging tool to include a JVM. You end up with a complete self-contained self-launchable application.

This means you need multiple builds, one app for each platform your users may deploy on (macOS, Linux, BSD, Windows, and so on). While that is an additional burden to you, the flip-side is that you control exactly what version of Java is being used to run your app.

Because of the Java Platform Module System (JSR 376) in Java 9 and later, you can now strip down the bundled JVM and libraries to include only the parts actually used by your particular app.

The build tools for packaging your app have been rapidly evolving in recent years. So be sure to do your research to find the most robust and modern tooling.

Alternatives
OpenWebStart

You might be interested in a separate implementation of Java Web Start technology.

While Oracle is phasing out Java Web Start, there is an open-source implementation of JSR 56 called OpenWebStart. See GitHub. This project is currently maintained by the company Karakun, based on the IcedTea-Web core functionality developed at AdoptOpenJDK.

GraalVM

A cutting-edge alternative is to build an entirely native-code ahead-of-time compiled version of your app using GraalVM.

Vaadin Flow

An entirely different way to build a web app by using Java is the Vaadin Flow framework.

You specify your user-interface layouts with widgets in a manner quite similar to Swing. You can do so using your choice of straight Java code, an XML-based description language, or a visual design tool.

At run-time, Vaadin automatically automatically generates the HTML, CSS, and JavaScript necessary to render your app remotely on the client user’s machine within a web browser. So, you have pure Java on the server-side, and no Java on the client-side, just Web standards technologies built into all modern browsers.

More info

All of this has been covered many times already on Stack Overflow. So search to learn more.

For tips on obtaining a JVM to bundle with your app, see How to get java 11 run-time environment working since there is no more jre 11 for download?.

Question:

I would like to get rid of Java Deployment Toolkit when running applets. Actually my customer doesn't want to click activate in applet before it start running. It happens if the JRE has installed the DT, this is not my case, so I can't reproduce locally.

I am running my applet using the deployJava.js.

Is there a way to disable the Deployment Toolkit directly in my code? I don't to instruct my users to disable/remove DT from their browser.


Answer:

deployJava.js is the Deployment Toolkit. As far as I know, it is never installed in a browser. It is web content, and a page containing an applet will normally include the Deployment Toolkit with <script src="https://www.java.com/js/deployJava.js"></script> (or it can point to a bundled copy, such as <script src="deployJava.js"></script>).

You can place an applet in a page without using the Deployment Toolkit, by doing away with deployJava.js altogether, and simply writing HTML that embeds the applet:

<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
        codebase="http://java.sun.com/update/1.7.0/jinstall-7u67-windows-i586.cab"
        width=400 height=200>
    <param name="type" value="application/x-java-applet;version=1.7">
    <param name="archive" value="SimpleApplet.jar">
    <param name="code" value="com/example/SimpleApplet.class">

    <object codetype="application/x-java-applet;version=1.7"
            archive="SimpleApplet.jar"
            classid="java:com.example.SimpleApplet"
            width=400 height=200>

            Java plug-in not installed.
    </object>

</object>

The outer <object> renders in Internet Explorer. Browsers which don't know how to render an <object> element will fall back to rendering its nested content, which in this case is the inner <object> element. See the documentation of <object> for more information.

If your applet is using a JNLP file, specify it in both <object> elements:

<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
        codebase="http://java.sun.com/update/1.7.0/jinstall-7u67-windows-i586.cab"
        width=400 height=200>
    <param name="type" value="application/x-java-applet;version=1.7">
    <param name="archive" value="SimpleApplet.jar">
    <param name="code" value="com/example/SimpleApplet.class">
    <param name="jnlp_href" value="SimpleApplet.jnlp">

    <object codetype="application/x-java-applet;version=1.7"
            archive="SimpleApplet.jar"
            classid="java:com.example.SimpleApplet"
            width=400 height=200>
        <param name="jnlp_href" value="SimpleApplet.jnlp">

        Java plug-in not installed.
    </object>

</object>