Search code examples
javaswingsyntaxjoptionpane

Java swing not recognized


I am just starting out in Java but have hit problems in multiple tutorials when it comes to using swing. This sample code from Java for Dummies threw the following error:

helloapp.java:

public class helloapp
{
    public static void main(String[] args)
    {
        greeter myGreeterObject = new greeter();
        myGreeterObject.sayhello();
    }
}

greeter.java

import javax.swing.JOptionPane;

public class greeter
{
    public void sayhello()
    {
        //System.out.println("Hello, World!");
        JOptionPane.showMessageDialog(null, <<Hello, World!>>, 
<<greeter>>, JOptionPane.INFORMATION_MESSAGE);
    }
}

The thrown error was this upon compile:

    c:\>javac helloapp.java greeter.java
greeter.java:8: error: illegal start of expression
                JOptionPane.showMessageDialog(null, <<Hello, World!>>, <<greeter
>>, JOptionPane.INFORMATION_MESSAGE);
                                                    ^
greeter.java:8: error: ')' expected
                JOptionPane.showMessageDialog(null, <<Hello, World!>>, <<greeter
>>, JOptionPane.INFORMATION_MESSAGE);
                                                                  ^
greeter.java:8: error: illegal start of expression
                JOptionPane.showMessageDialog(null, <<Hello, World!>>, <<greeter
>>, JOptionPane.INFORMATION_MESSAGE);
                                                                   ^
greeter.java:8: error: ';' expected
                JOptionPane.showMessageDialog(null, <<Hello, World!>>, <<greeter
>>, JOptionPane.INFORMATION_MESSAGE);
                                                                     ^
greeter.java:8: error: illegal start of expression
                JOptionPane.showMessageDialog(null, <<Hello, World!>>, <<greeter
>>, JOptionPane.INFORMATION_MESSAGE);
                                                                       ^
greeter.java:8: error: ';' expected
                JOptionPane.showMessageDialog(null, <<Hello, World!>>, <<greeter
>>, JOptionPane.INFORMATION_MESSAGE);

^
greeter.java:8: error: not a statement
                JOptionPane.showMessageDialog(null, <<Hello, World!>>, <<greeter
>>, JOptionPane.INFORMATION_MESSAGE);

               ^
greeter.java:8: error: ';' expected
                JOptionPane.showMessageDialog(null, <<Hello, World!>>, <<greeter
>>, JOptionPane.INFORMATION_MESSAGE);

                                   ^
8 errors

This tutorial gave me similar errors which uses swing but frames instead of panes: http://zetcode.com/tutorials/javagamestutorial/basics/

The problems occurred the same on a Windows 7 and XP machine, latest JDK and JRE were used and the 32bit version was used for XP and the 64bit on the 7 system. I did include the environment variables path and can successfully use basic things like System.out.println . The XP machine has had previous versions of the JDK and JRE but they were unintalled prior. I have not tried reinstalling the OS but I'm thinking thats not it as the Win7 system has had a fresh install recently and throws the same problem. No other major progs were running during any of this. I used notepad for the progs.

I appreciate any help. I'm not sure what else to do here other than switch languages. I'm hoping its just something simple and trivial I've overlooked...


Solution

  • import javax.swing.JOptionPane;
    
    public class greeter
    {
        public void sayhello()
        {
            //System.out.println("Hello, World!");
            JOptionPane.showMessageDialog(null, "Hello, World!", 
    "greeter", JOptionPane.INFORMATION_MESSAGE);
        }
    }
    

    String should be in "" and not << >>