Reputation: 2566
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
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)
Click Finish.
Create the applet source file
NewJApplet
.javaapplication
.The IDE creates the applet source file in the specified package. The applet source file opens and the GUI Editor opens.
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;
}
Build
In the dist folder now is MyTestApplet.jar
MyTestApplet
MyTestApplet
use this html file.
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>
http://localhost/MyTestApplet/AppletPage.html
and voila:
Hope this helps.
Upvotes: 2