1

I'm brand new to java and have tried to get this to work for the last 48 hours and I'm about to give up.

I want to put the java applet on a website. Works fine only in eclipse. I tried many solutions already suggested on this site and none of them worked for me and just mucked up the code so I've reverted it to my original state. Could anyone pinpoint the problem? Thanks!

(code edited to reflect suggested answers)

    package nameapp;

    import java.util.*;
    import java.io.*;
    import java.applet.Applet

    public class NameApp extends Applet{

public static void main(String[] args) throws IOException {
    String name;
    BufferedReader reader;
    reader = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("What is your name? ");
    name = reader.readLine();
    if (name.equals("Mike"))  {
        System.out.print("Hello it's ");
        System.out.println(new Date());
        System.out.print("My name is ");
        System.out.print(name);
        System.out.println(" and I am totally awesome!!!");     
    }
    else if (name.equals("Lisa")) {
        System.out.print("Hello it's ");
        System.out.println(new Date());
        System.out.print("My name is ");
        System.out.print(name);
        System.out.println(" and I'm the prettiest gal in the world!");
    }
    else  {
        System.out.print("Hello it's ");
        System.out.println(new Date());
        System.out.print("My name is ");
        System.out.print(name);
        System.out.println(" and I'm just ok i guess...");
    }
}

}

And html is...

  <applet code=nameapp/NameApp.class width=300 height=300>
<param name="bgcolor" value="ffffff">
<param name="fontcolor" value="000000">
Your browser is not Java enabled.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
mike
  • 11
  • 3

4 Answers4

1

please read this tutorial that's contains basic stuff about JApplet,

please use JApplet not Applet

carrefully read signed java applet restrictions

What Applets Can and Cannot Do

https://stackoverflow.com/tags/java-web-start/info

note with Java 1.6.025 comings another restrictions for JApplet, and these problems and possible workaround are detailed described on this forum by Andrew Thompson, but I lost link ...

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Thanks for these links. I will investigate now. Right off the bat, do I need to use Swing? I'm still copying and pasting about 75% of my code so jumping into more libraries is a bit scary now. And do I need to sign it? (i tried for several hours to do this in eclipse with no success) In the end it was my understanding that applets need to be signed only if they access your computer. – mike May 18 '11 at 17:08
  • yes you'd use Swings JComponents but for initialize JApplent you miss public void init() {... – mKorbel May 18 '11 at 17:31
1

It looks like you are writing an application rather than an applet. When you run it in eclipse, do you select Run As... and then select Java Application? Try running it as a Java Applet instead. You should see appletviewer pop up with no content in it.

The entry point for an applet is the init() method, not main(), and the graphics related method paint() is also usually overloaded; I haven't seen an applet that has access to standard in and out.

You might find the stackoverflow question here useful: Main vs init.

Community
  • 1
  • 1
Atreys
  • 3,741
  • 1
  • 17
  • 27
  • Aha! This is correct. I understand about half of what you said but Ill look at that link now. Thanks – mike May 18 '11 at 17:10
0
code="nameapp/NameApp.class"

Put it would also need to extend java.applet.Applet and generally be an applet.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
  • So I did both those things.. added import java.applet.Applet and redid the first line to: public class NameApp extends Applet { I get a warning in eclipse The serializable class NameApp does not declare a static final serialVersionUID field of type long - Breakpoint:NameApp and in the html file i get an error and in the details I get nameapp/NameApp.class not found. any further ideas? – mike May 18 '11 at 16:34
  • The serialisation warning can be ignored (either do as it says, or add `@SuppressWarnings("serial")` (I think) before `public class`). The class files will need to be in the correct directory. If your html file is in http://example.xx/me/myapplet.html, your class file should be in http://example.xx/me/nameapp/NameApp.class. For an applet overriding init (`@Override public void init() {`) is much the same as an application's `public static void main(String[] args) {`. – Tom Hawtin - tackline May 18 '11 at 16:54
  • Thanks Tom. I'm doing my best to understand your code suggestions (remember im brand new to this). placing the class file in a folder called nameapp did something. It made the applet go away entirely (no box with error icon). Do you think theres more to be done on the directory? Is the @override necessary? If so could you explain further? – mike May 18 '11 at 17:02
  • `@Override` just makes sure that you are overriding the method (and haven't misspelled it, go the parameters wrong or something). Your `System.out.println`s will go to the Java Console (which you can open somewhere in the Java Control Panel. However, applets are for GUI apps, so you'll need to add a `TextArea` and `TextField` or something. – Tom Hawtin - tackline May 18 '11 at 20:37
0

Read the Applet tutorial - Instead of a public static void main(String[] args) method, an Applet needs public void init().

Also, with the code you have now, you'll just see a blank Applet - you'll have to have the Java Debug Console up to see anything printed by System.out.println(), and Applets can't access System.in to read input - instead you'll want to add some TextField components to your Applet and read and write the text with those.

Nate
  • 16,748
  • 5
  • 45
  • 59
  • Ok I see now that there is a difference between applet and java app. I dont know quite what im doing but I'll see if I can learn this bit now thabks – mike May 18 '11 at 17:12