Question

I've tried my best to research this myself, but cannot get anywhere - I keep getting the same errors. I'm compiling from terminal with the javac command, and get the errors -

javaJOptionPane.java:1: JOptionPane is already defined in this compilation unit
import javax.swing.JOptionPane;
^
javaJOptionPane.java:7: cannot find symbol
symbol  : method showInputDialog(java.lang.String)
location: class JOptionPane
        String input = JOptionPane.showInputDialog("Enter Input");
                              ^
2 errors

for this code -

import javax.swing.JOptionPane;

class JOptionPane
{
    public static void main()
    {
        String input = JOptionPane.showInputDialog("Enter Input");
        System.out.println(input);
    }
}

Using the command "javac filename.java". Sorry if this has already been answered; I can't find it, and am rather stuck!

Était-ce utile?

La solution

This is because you are trying to redefine an already defined class JOptionPane , just rename your class and it should be fine

class JOptionPaneDemo
{
     // continue

Also since you'd have to define this in "JOptionPaneDemo.java", you can compile and run this as

javac JOptionPaneDemo.java
java JOptionPaneDemo

Autres conseils

Change your class name to something else - JOptionTest. You are actually confusing the compiler with same name. so, it looks for the method - showInputDialog in your class instead of in JOptionPane class in JDK

javac JOptionTest.java // to compile it
java JOptionTest // to run it
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top