Hot questions for Using Applets in firefox

Question:

I have a Java web application that needs to know the computer name of clients connecting to it. My first idea was to get it via JavaScript and fill a hidden form field with it, but after some digging, it appears JS cannot access that information.

Then I tried using an applet and accessing the applet methods via JavaScript. This seems to work on Firefox, but newer Chrome versions don't run applets.

Then I considered switching to a Java Webstart application, which, as far as I know, should work under Chrome, but in this scenario, since the Webstart application runs outside the browser,JavaScript cannot access its methods.

Then I tried saving the hostname in the environment TEMP directory, which works, in Firefox + Linux + Java7, but not in Firefox + Windows + Java8: the applet just doesn't run, and, in addition, I haven't found a way to access the defined TEMP directory and read the file in JavaScript.

At this point I'm out of ideas and would love to have some input from you guys. Any hints on how to achieve this? Did I miss any obvious solution?

Please notice I need the computer defined hostname, not what the computer's IP resolves to via DNS.

Thanks.


Answer:

Your Javawebstartet Application could Host Websocket listener. So you could access this application via Websocket from javascript. Works only with http, not https

On JavaSide use the websocket implementation of http://java-websocket.org/

In Javascript I use https://code.google.com/p/jquery-websocket/ You can find examples there too. The websocket communication is async. Create the WS with the callback method for the response

var ws = $.websocket("ws://127.0.0.1:" + lport + "/", {
            events: {
                say: function (e) {
                    //showMsg(e.data);

                }
            }
        });

and call the server with

ws.send(0, jsonData)

Question:

Hello I am trying to load applet on web browser. I have create html and include applet tag

<applet code="com.asprise.imaging.scan.ui.demo.PanelMain.class"   
 codebase="."archive="asprise-imaging-ui.jar,asprise-imaging-
 scan-core.jar" width="400" height="300">

I have jar file also in same folder where i created my html,it gives me error.

This plug-in is not supported

Kindly help me to solve this issue.

Thank you.


Answer:

In Eclipse select the main applet file right click and export it to runnable jar. Let all the dependent jar's bundled. Then in command prompt browse to the folder your jar is placed and type the following commands.

1.keytool -genkey -keyalg rsa -alias m4key  //m4key unique key
2.keytool -export -alias m4key -file bmcert.crt   //bmcert.crt certificate name
3.jar tvf Jarname.jar verify jar // jarname.jar is the jar file created
4.jarsigner Jarname.jar m4key
5.jarsigner -verify -verbose -certs JarnameJarname.jar 

Then simply use the html script and in code property specify the main file of Applet.

<html>
<applet width="1100" height="600" archive="EnrollmentApplet.jar" code="com/vaazu/biometric/Applet/EnrollmentApplet.class"></applet>             
</html>

If you are using java8 ,the security issue by Java 8, Which is not allowing now to display or load the self signed Applets on browser. Please open configure java in settings and in security TAB put the URL of the html page which is using applet in Exception Site List

Question:

I am using qzprint API for printing labels in my open cart extension. Everything was working fine but suddenly it stopped working on FF. In Internet explorer it works fine. If i add alerts in my functions of applet it works fine on firefox as well but not sure why not with out alerts. here is my code.

calling applet functions in my header.tpl

<script type="text/javascript">
deployQZ('<?php echo HTTP_CATALOG ?>');
useDefaultPrinter();
<script>

Applet file containing functions

function deployQZ(path) {
//alert("alert for printing label");
    pathApplet = path + 'java/qz-print.jar';
    pathJnlp = path + 'java/qz-print_jnlp.jnlp';
    var attributes = {id: "qz", code:'qz.PrintApplet.class', 
        archive: pathApplet, width:1, height:1};
    var parameters = {jnlp_href: pathJnlp, 
        cache_option:'plugin', disable_logging:'false', 
        initial_focus:'false'};
    if (deployJava.versionCheck("1.7+") == true) {}
    else if (deployJava.versionCheck("1.6+") == true) {
        attributes['archive'] = 'java/jre6/qz-print.jar';
        parameters['jnlp_href'] = 'java/jre6/qz-print_jnlp.jnlp';
    }
    deployJava.runApplet(attributes, parameters, '1.5');
}

/**
* Automatically gets called when applet has loaded.
*/
function qzReady() {
    // Setup our global qz object
    window["qz"] = document.getElementById('qz');
    //var title = document.getElementById("title");
    if (qz) {
        try {
            //title.innerHTML = title.innerHTML + " " + qz.getVersion();
            //document.getElementById("content").style.background = "#F0F0F0";
        } catch(err) { // LiveConnect error, display a detailed meesage
            document.getElementById("content").style.background = "#F5A9A9";
            alert("ERROR:  \nThe applet did not load correctly.  Communication to the " + 
                "applet has failed, likely caused by Java Security Settings.  \n\n" + 
                "CAUSE:  \nJava 7 update 25 and higher block LiveConnect calls " + 
                "once Oracle has marked that version as outdated, which " + 
                "is likely the cause.  \n\nSOLUTION:  \n  1. Update Java to the latest " + 
                "Java version \n          (or)\n  2. Lower the security " + 
                "settings from the Java Control Panel.");
      }
  }
}

    /**
* Returns is the applet is not loaded properly
*/
function isLoaded() {
    if (!qz) {
        alert('Error:\n\n\tPrint plugin is NOT loaded!');
        return false;
    } else {
        try {
            if (!qz.isActive()) {
                alert('Error:\n\n\tPrint plugin is loaded but NOT active!');
                return false;
            }
        } catch (err) {
            alert('Error:\n\n\tPrint plugin is NOT loaded properly!');
            return false;
        }
    }
    return true;
}

    function useDefaultPrinter() {
    //alert("alert for printing label");
    if (isLoaded()) {
        // Searches for default printer
        qz.findPrinter();

        // Automatically gets called when "qz.findPrinter()" is finished.
        window['qzDoneFinding'] = function() {
            // Alert the printer name to user
            var printer = qz.getPrinter();
            //alert(printer !== null ? 'Default printer found: "' + printer + '"':
                //'Default printer ' + 'not found');
            document.getElementById("name_printer").innerHTML = 'Default printer found: "' + printer + '"'; 
            // Remove reference to this function
            window['qzDoneFinding'] = null;
            defaultFound = true;
        };
    }
}

As u can see in my deployqz() and usedefaultprinter() functions i have alert on first line which is in comments if its commented it doesn't work in fire fox and if not commented than it works fine. With comments i get alert message from isLoaded() function "Print plugin is NOT loaded properly!".

Also in my console i get this

An unbalanced tree was written using document.write() causing data from the network to be reparsed. For more information https://developer.mozilla.org/en/Optimizing_Your_Pages_for_Speculative_Parsing


Answer:

Try this:

  1. If the qzReady is called by the applet when ready, put useDefaultPrinter inside that function.
  2. if isLoaded takes some time, call useDefaultPrinter in there too using setTimeout

Like this

<script type="text/javascript">
deployQZ('<?php echo HTTP_CATALOG ?>');
<script>

Applet file containing functions

var qz;

function deployQZ(path) {
    pathApplet = path + 'java/qz-print.jar';
    pathJnlp = path + 'java/qz-print_jnlp.jnlp';
    var attributes = {id: "qz", code:'qz.PrintApplet.class', 
        archive: pathApplet, width:1, height:1};
    var parameters = {jnlp_href: pathJnlp, 
        cache_option:'plugin', disable_logging:'false', 
        initial_focus:'false'};
    if (deployJava.versionCheck("1.7+") == true) {}
    else if (deployJava.versionCheck("1.6+") == true) {
        attributes['archive'] = 'java/jre6/qz-print.jar';
        parameters['jnlp_href'] = 'java/jre6/qz-print_jnlp.jnlp';
    }
    deployJava.runApplet(attributes, parameters, '1.5');
}

/**
* Automatically gets called when applet has loaded.
*/
function qzReady() {
    // Setup our global qz object
    qz = document.getElementById('qz');
    if (qz) {
        try {
          useDefaultPrinter();
        } catch(err) { // LiveConnect error, display a detailed meesage
            document.getElementById("content").style.background = "#F5A9A9";
            alert("ERROR:  \nThe applet did not load correctly.  Communication to the " + 
                "applet has failed, likely caused by Java Security Settings.  \n\n" + 
                "CAUSE:  \nJava 7 update 25 and higher block LiveConnect calls " + 
                "once Oracle has marked that version as outdated, which " + 
                "is likely the cause.  \n\nSOLUTION:  \n  1. Update Java to the latest " + 
                "Java version \n          (or)\n  2. Lower the security " + 
                "settings from the Java Control Panel.");
      }
   }
   else { setTimeout(useDefaultPrinter,300); }
}

    /**
* Returns is the applet is not loaded properly
*/
function isLoaded() {
    if (!qz) {
        alert('Error:\n\n\tPrint plugin is NOT loaded!');
        return false;
    } else {
        try {
            if (!qz.isActive()) {
                alert('Error:\n\n\tPrint plugin is loaded but NOT active!');
                return false;
            }
        } catch (err) {
            alert('Error:\n\n\tPrint plugin is NOT loaded properly!');
            return false;
        }
    }
    return true;
}

function useDefaultPrinter() {
    //alert("alert for printing label");
    if (isLoaded()) {
        // Searches for default printer
        qz.findPrinter();

        // Automatically gets called when "qz.findPrinter()" is finished.
        window['qzDoneFinding'] = function() {
            // Alert the printer name to user
            var printer = qz.getPrinter();
            //alert(printer !== null ? 'Default printer found: "' + printer + '"':
                //'Default printer ' + 'not found');
            document.getElementById("name_printer").innerHTML = 'Default printer found: "' + printer + '"'; 
            // Remove reference to this function
            window['qzDoneFinding'] = null;
            defaultFound = true;
        };
    }
    else { setTimeout(useDefaultPrinter,300); }
}

Question:

I wish to override behavior of FileDialog call of awt library so as to not invoke any native calls to open up file chooser. I am planning to achieve it by overriding FileDialog with a stub library and forcing it to load before standard ones whenever an applet is loaded using icedTea firefox java plugin.


Answer:

For an intermittent fix, I have come up with a hack to replace standard JRE with custom one, wherein FileDialog's show method is overridden to a no-op. Then, the location of custom JRE is specified in ~/.icedtea/deployment.properties file using custom.jre property.(Please confirm property name for your version of icedtea plugin by referring source code.)

Question:

when I run the following code on Eclipse (Luna, java vers=8), the code runs and pops-up the two errors messages. On the other hand, when I embed the code in a html page the code shows only the first error message. It seems that calling the ForkJoinPool class crushes the applet on firefox. Do you know why? Here is the code.

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ForkJoinPool;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ProvaVera extends JApplet
{
    public void start() 
    {

        SwingUtilities.invokeLater(new Runnable(){
        public void run() 
            {
                MainPanel panel = new MainPanel();      
                // Add Swing components to content pane
                Container c = getContentPane();
                c.add(panel, BorderLayout.CENTER);
            }
        });

    }

}
class MainPanel extends JPanel
{

    public MainPanel()
    {
        JLabel label1 = new JLabel("label1");
        this.add(label1);
        JButton btn1 = new JButton("button1");
        this.add(btn1);

        btn1.addActionListener  (
                new ActionListener() 
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        metodo();
                    }
                }

                );
    }



    public void metodo()
    {
        JOptionPane.showMessageDialog(new JFrame(), "test1", "Dialog", JOptionPane.ERROR_MESSAGE);
        ForkJoinPool pool = new ForkJoinPool();
        JOptionPane.showMessageDialog(new JFrame(), "test2", "Dialog", JOptionPane.ERROR_MESSAGE);
    }
}

Answer:

You are probably getting a security exception because ForkJoinPool requires the 'modifyThread' runtime permission.

When you run in Eclipse there is no security manager installed so you don't get security exceptions. When you run in a browser there is a strict security manager.