We help IT Professionals succeed at work.

Java Applet wont load on HTML file

Lost_User
Lost_User asked
on
Hi,

I am trying to upload my java that i have worked on and it is throwing an excepection.

Jave: Helloworld.java
 
class Helloworld {
	 public static void main(String args[]){
	     System.out.println("Hello World");
	  }
 }  

Open in new window


HTML:
<html>

<body>
<h2>My first Java program</h2>
	<applet code="Helloworld.class" height="200" width="250">
	</applet>
</body>
</html>

Open in new window


Error:
" Application Error
 RuntimeException
 java.lang.reflect.InvocationTargetException"

Open in new window


I am able to compile the java view it in console but when I load the html file it throws the above error.
Comment
Watch Question

Author

Commented:
I have also used this java with no result.

 import java.applet.Applet;
 import java.awt.*;
 
 class Helloworld extends Applet{
	 public static void main(String args[]){
	     System.out.println("Hello World");
	  }
 }  

Open in new window

Commented:
Applets are not simple java programs. There is a tutorial you should read

Author

Commented:
I have used that also and it throws the same error...

http://docs.oracle.com/javase/tutorial/deployment/applet/getStarted.html

Author

Commented:
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;

public class Helloworld extends JApplet {
    //Called when this applet is loaded into the browser.
    public void init() {
        //Execute a job on the event-dispatching thread; creating this applet's GUI.
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    JLabel lbl = new JLabel("Hello World");
                    add(lbl);
                }
            });
        } catch (Exception e) {
            System.err.println("createGUI didn't complete successfully");
        }
    }
}

Open in new window


Failed to run due to "Exception in thread "main" java.lang.NoSuchMethodError: main"
IT Business Systems Analyst / Software Developer
CERTIFIED EXPERT
Top Expert 2015
Commented:
Failed to run due to "Exception in thread "main" java.lang.NoSuchMethodError: main"

Was the above error whilst trying to run it in a console?

Again, as stated above, java programs and applets are two different things. You run java programs (those that have "main" functions) in the console, etc and you run applets (those that extend from JApplet and such) from the html in web pages. The other way around will not work!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.