Error: Can not find or load the FrameDemo main class?

advertisements

Here is my code,

package components;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/* FrameDemo.java requires no other files. */
public class FrameDemo {
/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("FrameDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel emptyLabel = new JLabel("");
    emptyLabel.setPreferredSize(new Dimension(175, 100));
    frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}
}

I copied this from the oracle website with copy and paste verbatim. Here

And here is what I am doing in command prompt? What is the problem here. I am seriously at my wiits end.

Directory of C:\Users\headgearxthree\Desktop\SCRAP\JAVA\compile\gui

12/17/2016  12:42 PM    <DIR>          .
12/17/2016  12:42 PM    <DIR>          ..
12/17/2016  12:29 PM             2,765 FrameDemo.java
           1 File(s)          2,765 bytes
           2 Dir(s)  51,945,787,392 bytes free

C:\Users\headgearxthree\Desktop\SCRAP\JAVA\compile\gui>javac Framedemo.java

C:\Users\headgearxthree\Desktop\SCRAP\JAVA\compile\gui>java -cp . FrameDemo
Error: Could not find or load main class FrameDemo

C:\Users\headgearxthree\Desktop\SCRAP\JAVA\compile\gui>

I made a Hello World! program just before in its parent directory with no problems. What is this error? What is going wrong? These examples should be simple and eloquent. This is a simple how to and yet as soon as graphics are involved all programming on windows goes to sh*t. Wtf? This is not a duplicate. All similar questioned reference a specific instance. I have phrased this in a none partisan fashion so it can help the many. Please do not lock.


The class is in package components This means that

  • its fully qualified name is components.FrameDemo, not FrameDemo.
  • its source file should be in a directory named components: the structure of directories should match the structure of the packages.

It's also a bad idea to mix source files and class files in the same directory. You should put your sources under an src directory, and classes under a separate classes directory:

mkdir src
mkdir classes
mkdir src/components
mv FrameDemo.java src/components
javac -d classes src/components/FrameDemo.java
java -cp classes components.FrameDemo

Note that, if you has read the tutorial properly and downloaded the whole project zip file from https://docs.oracle.com/javase/tutorial/uiswing/examples/components/, you would have the right structure from the start.