Boyang
Boyang

Reputation: 2566

Unable to deploy any applet built by NetBeans

I'm new to Java. I'm unable to deploy any applet on a html page and constantly getting InvocationTargetException. To illustrate this, here is an example:

My applet has only one class that has a main. All the code are generated by NetBeans and the GUI contains only one button:

package javaapplication;

public class NewJFrame extends javax.swing.JFrame {
    public NewJFrame() {
        initComponents();
    }

    @SuppressWarnings("unchecked")


   // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("jButton1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addContainerGap(317, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addContainerGap(266, Short.MAX_VALUE))
        );

        pack();
    }
    public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewJFrame().setVisible(true);
        }
    });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    // End of variables declaration      
}

It compiles and runs smoothely, within NetBeans. Now I clean and build the .jar file. Here's the MANIFEST.MF, which looks fine to me:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.4
Created-By: 1.7.0_10-b18 (Oracle Corporation)
Class-Path: 
X-COMMENT: Main-Class will be added automatically by build
Main-Class: javaapplication.NewJFrame

Here is my index.html:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <applet archive="JavaApplication.jar" width="300" height="300" code="javaapplication.NewJFrame"/>

    </body>

</html>

I've put them in the same directory and set permission properly as well. Now when I try to open my index.html which is placed on server, it gives InvocationTargetException, on both newest versions of Firefox and Chrome.

I'm tried many different things like creating a another class that has a main, or using Jnlp but no avail. Any help is greatly appreciated.

Upvotes: 1

Views: 947

Answers (1)

moskito-x
moskito-x

Reputation: 11968

I can not see an applet in your code.

package javaapplication;

public class NewJFrame extends javax.swing.JFrame {
    public NewJFrame() {
        initComponents();
    }
[...]

Create the Java project from scratch. (Netbeans)

  • Choose File > New Project (Ctrl-Shift-N) (MyTestApplet).
  • Under Categories, select Java.
  • Select Java Class Library under Projects.
  • Click Finish.

  • Create the applet source file

  • Right-click the MyTestApplet project node in the Projects window and select New > Other (Ctrl-N).
  • Under Categories, select Java.
  • Select Swing GUI Forms > JApplet Form.
  • Click Next.
  • Under Class Name, type NewJApplet .
  • Under Package, type javaapplication .
  • Click Finish.

The IDE creates the applet source file in the specified package. The applet source file opens and the GUI Editor opens.

  • Drag an Button to the Gui.

The Source NewJapplet.java should look something like:

package javaapplication;

public class NewJApplet extends javax.swing.JApplet {


    @Override
    public void init() {

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    @SuppressWarnings("unchecked")

    private void initComponents() {

        jButton1 = new javax.swing.JButton();

        jButton1.setText("jButton1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(182, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(139, 139, 139))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addComponent(jButton1)
                .addContainerGap(232, Short.MAX_VALUE))
        );
    }
    private javax.swing.JButton jButton1;

}
  • Run Build

In the dist folder now is MyTestApplet.jar

  • Copy the .jar to the server in a folder MyTestApplet
  • Copy following .html to the server, same folder MyTestApplet

use this html file.

  • Change classid="clsid:CAFEEFAC-0016-0000-0000-ABCDEFFEDCBA" width="300" height="300"> pointing to your Java Version
  • 0017

AppletPage.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en-US">
  <head>
    <title>Applet Page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
  </head>
  <body>
    <h1>Applet Demo</h1>
    <h2>This applet has been deployed with the object tag</h2>
    <object 
    classid="clsid:CAFEEFAC-0016-0000-0000-ABCDEFFEDCBA" width="300" height="300">
    <PARAM name="code" value="javaapplication.NewJApplet">
    <PARAM name="archive"  value="MyTestApplet.jar">
    <comment>
      <embed code="javaapplication.NewJApplet.class"
             width="300"
             height="300"
             archive="MyTestApplet.jar"
             type="application/x-java-applet">
        <noembed>
          No Java Support.
        </noembed>
      </embed>
    </comment>
  </object>

  </body>
</html>
  • in your browser type the url: http://localhost/MyTestApplet/AppletPage.html

and voila:

enter image description here

Hope this helps.

Upvotes: 2

Related Questions