0

I have a simple Hello.java class which I want to put in a website.

 public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

I tried to do

jar cf Hello.jar Hello.java

and then in the site to try to get it to run I put

<h2> Hello Test </h2>
<APPLET 
   CODE="Hello.class"
   WIDTH="50%" HEIGHT="50"
   ARCHIVE = "Hello.jar"
> This example uses a Hello.jar applet.
</APPLET>

Needless to say it isn't working.

2

2 Answers 2

6

Maybe you should inherit from Applet?

EDIT: Something on the line of:

public class FirstApplet extends Applet
{
    public FirstApplet ()
    {
        setBackground (Color.BLUE);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Extending from JApplet would be better.
0

If you write a applet, it should have another structure than a stand-alone app, this is because you have other environments inside browser than you have stand-alone.

When running your app as an applet, you have a fixed screen, and you can't send text to it, and can basically use only the screenspace the browser has provided.

When running it as stand alone, you basicly have more power, and can access more things without security exception, but most importantly, you also need to do the graphical user interface things by yourself.

Example:

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");
            }
        }
      }

Example from: Oracle Aplet Getting started (Requires java plugin to view)

Create a jar from it using:

javac HelloWorld.jar
jar cf Hello.jar Hello.class

Then run it in browser using:

<h2> Hello Test </h2>
<APPLET 
   CODE="HelloWorld"
   WIDTH="50%" HEIGHT="50"
   ARCHIVE = "Hello.jar"
 > This example uses a Hello.jar applet.</APPLET>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.