I know Java need main function and I have one. This code compile with no error (This code is source code of the Design Pattern book) ,but when I execute
D:\ java SwingObserverExample
Error: Could not find or load main class SwingObserverExample
here is source code:
package headfirst.designpatterns.observer.swing;
import java.awt.*;
import javax.swing.*;
public class SwingObserverExample{
JFrame frame;
public static void main(String[] args) {
SwingObserverExample example = new SwingObserverExample();
example.go();
}
public void go() {
frame = new JFrame();
JButton button = new JButton("Should I do it?");
// Without lambdas
//button.addActionListener(new AngelListener());
//button.addActionListener(new DevilListener());
// With lambdas
button.addActionListener(event ->
System.out.println("Don't do it, you might regret it!")
);
button.addActionListener(event ->
System.out.println("Come on, do it!")
);
frame.getContentPane().add(BorderLayout.CENTER, button);
// Set frame properties
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(BorderLayout.CENTER, button);
frame.setSize(300,300);
frame.setVisible(true);
}
/*
* Remove these two inner classes to use lambda expressions instead.
*
class AngelListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
System.out.println("Don't do it, you might regret it!");
}
}
class DevilListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
System.out.println("Come on, do it!");
}
}
*/
why java can't find this program's main class ?
directory of my java file is D:\workplace\Head-First-Design-Patterns-master\Head-First-Design-Patterns-master\src\headfirst\designpatterns\observer\swing
and I run java command from there.