1

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.

Esterlinkof
  • 1,444
  • 3
  • 22
  • 27

3 Answers3

2

Include the package name in the run command

java headfirst.designpatterns.observer.swing.SwingObserverExample

Also Java expects to find the class in a directory headfirst\designpatterns\observer\swing relative to the current location to match the package name so run the application from

D:\workplace\Head-First-Design-Patterns-master\Head-First-Design-Patterns-maste‌​‌​r\src
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • You havent said what your directory structure is like but Java expects the class to be in a directory `headfirst\designpatterns\observer\swing` relative to the current location.... – Reimeus Mar 23 '15 at 19:30
  • my directory is `D:\workplace\Head-First-Design-Patterns-master\Head-First-Design-Patterns-master\src\headfirst\designpatterns\observer\swing` – Esterlinkof Mar 23 '15 at 19:31
  • In that case you can use a runtime location of `D:\workplace\Head-First-Design-Patterns-master\Head-First-Design-Patterns-maste‌​r\src` – Reimeus Mar 23 '15 at 19:33
  • It's worked. please include this in your answer and I will mark your answer as solution. It's better to include link about java package tutorial. Thanks again – Esterlinkof Mar 23 '15 at 19:37
  • I have irrelevant question (about you), I beg leave to say in this comment ? – Esterlinkof Mar 23 '15 at 19:45
1

This is because of the line package headfirst.designpatterns.observer.swing. The class is compiled in the package headfirst.designpatterns.observer.swing.

Either remove that or (better) run it using

java headfirst.designpatterns.observer.swing.SwingObserverExample
John Bupit
  • 10,406
  • 8
  • 39
  • 75
0

While executing the code using java command use classpath - cp option and add the fully qualified class name where the main method exist -

java -cp headfirst.designpatterns.observer.swing.SwingObserverExample
Razib
  • 10,965
  • 11
  • 53
  • 80