Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By gimbal2

Thread: Could not find or load main class (half the time)?

  1. #1
    Drew345 is offline Member
    Join Date
    May 2015
    Posts
    31
    Rep Power
    0

    Default Could not find or load main class (half the time)?

    I am learning Swing now with the book from Oracle, “Java A beginners guide”.
    All of the swing programs work fine in Eclipse. But when I try using the DOS prompt and JRE tools (right wording?) all Swing Program compile fine, but then half will fail to run with
    Error: Could not find or load main class ***.java.

    I compile and run as follows:
    >javac SwingDemo.java
    >java SwingDemo

    After the javac command, two class files will be created, SwingDemo.class, and SwingDemo$1.class.
    After the java command, half the files run and half fail. All files run fine in Eclipse and all are copied directly from the Oracle website.

    Any idea why these first two programs run fine and last two fail with “Error: Could not find or load main class ***.java “. All class files exist in the directory after running javac. Looking at the directory, all 4 of these cases have all necessary files.

    SwingDemo.java (runs fine)
    ButtonDemo.java (runs fine)
    ListDemo.java (Error: Could not find or load main class…)
    SwingFC.java (Error: Could not find or load main class…)
    (All have class files present in the directory after javac)

    SwingDemo.java -- -- -- -- -- -- -- -- -- -- -- -- - -- -

    Java Code:
    import javax.swing.*; 
      
    class SwingDemo { 
     
      SwingDemo() { 
     
        // Create a new JFrame container. 
        JFrame jfrm = new JFrame("A Simple Swing Application"); 
     
        // Give the frame an initial size. 
        jfrm.setSize(275, 100); 
     
        // Terminate the program when the user closes the application. 
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     
        // Create a text-based label. 
        JLabel jlab = new JLabel(" Swing defines the modern Java GUI."); 
     
        // Add the label to the content pane. 
        jfrm.add(jlab); 
     
        // Display the frame. 
        jfrm.setVisible(true); 
      } 
     
      public static void main(String args[]) { 
        // Create the frame on the event dispatching thread. 
        SwingUtilities.invokeLater(new Runnable() { 
          public void run() { 
            new SwingDemo(); 
          } 
        }); 
      } 
    }
    ButtonDemo.java -- -- -- -- -- -- -- -- -- -- -- - --

    Java Code:
    //Demonstrate a push button and handle action events. 
    
    import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; 
    
    class ButtonDemo implements ActionListener { 
    
    JLabel jlab;  
    
    ButtonDemo() { 
    
     // Create a new JFrame container. 
     JFrame jfrm = new JFrame("A Button Example"); 
    
     // Specify FlowLayout for the layout manager. 
     jfrm.setLayout(new FlowLayout()); 
    
     // Give the frame an initial size. 
     jfrm.setSize(220, 120); 
    
     // Terminate the program when the user closes the application. 
     jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    
     // Make two buttons. 
     JButton jbtnUp = new JButton("Up"); 
     JButton jbtnDown = new JButton("Down"); 
    
     // Add action listeners. 
     jbtnUp.addActionListener(this); 
     jbtnDown.addActionListener(this); 
    
     // Add the buttons to the content pane. 
     jfrm.add(jbtnUp);  
     jfrm.add(jbtnDown);  
    
     // Create a label. 
     jlab = new JLabel("Press a button."); 
    
     // Add the label to the frame. 
     jfrm.add(jlab); 
    
     // Display the frame. 
     jfrm.setVisible(true); 
    } 
    
    // Handle button events. 
    public void actionPerformed(ActionEvent ae) { 
     if(ae.getActionCommand().equals("Up"))  
       jlab.setText("You pressed Up."); 
     else 
       jlab.setText("You pressed down. "); 
    } 
    
    public static void main(String args[]) { 
     // Create the frame on the event dispatching thread. 
     SwingUtilities.invokeLater(new Runnable() { 
       public void run() { 
         new ButtonDemo(); 
       } 
     }); 
    } 
    }
    ListDemo.java-- -- -- -- -- -- -- -- -- -- -- -- - -- -- -- -- -- -- - ---

    Java Code:
    import javax.swing.*;  
    import javax.swing.event.*; 
    import java.awt.*; 
    import java.awt.event.*; 
       
    class ListDemo implements ListSelectionListener {  
      
      JList<String> jlst; 
      JLabel jlab; 
      JScrollPane jscrlp; 
     
      // Create an array of names. 
      String names[] = { "Sherry", "Jon", "Rachel",  
                         "Sasha", "Josselyn",  "Randy", 
                         "Tom", "Mary", "Ken", 
                         "Andrew", "Matt", "Todd" }; 
     
      ListDemo() {  
        // Create a new JFrame container.  
        JFrame jfrm = new JFrame("JList Demo");  
     
        // Specify a flow Layout. 
        jfrm.setLayout(new FlowLayout());  
     
        // Give the frame an initial size.  
        jfrm.setSize(200, 160);  
      
        // Terminate the program when the user closes the application.  
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
      
        // Create a JList. 
        jlst = new JList<String>(names); 
     
        // Set the list selection mode to single-selection. 
        jlst.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
     
        // Add list to a scroll pane. 
        jscrlp = new JScrollPane(jlst); 
     
        // Set the preferred size of the scroll pane. 
        jscrlp.setPreferredSize(new Dimension(120, 90)); 
     
        // Make a label that displays the selection. 
        jlab = new JLabel("Please choose a name"); 
     
        // Add list selection handler. 
        jlst.addListSelectionListener(this); 
     
        // Add the list and label to the content pane. 
        jfrm.add(jscrlp); 
        jfrm.add(jlab); 
      
        // Display the frame.  
        jfrm.setVisible(true);  
      }  
     
      // Handle list selection events. 
      public void valueChanged(ListSelectionEvent le) {  
        // Get the index of the changed item. 
        int idx = jlst.getSelectedIndex(); 
     
        // Display selection, if item was selected. 
        if(idx != -1) 
          jlab.setText("Current selection: " + names[idx]); 
        else // Othewise, reprompt. 
          jlab.setText("Please choose an name"); 
      }  
     
      public static void main(String args[]) {  
        // Create the frame on the event dispatching thread.  
        SwingUtilities.invokeLater(new Runnable() {  
          public void run() {  
            new ListDemo();  
          }  
        });   
      }  
    }
    SwingFC.java -- -- -- -- -- -- -- - -- -- - -- -- -- -- -- -- -- - -- -

    Java Code:
    package chpt15;
    
    /*
    Try This 15-1
    
    A Swing-based file comparison utility.
    
    Requires JDK 7 or later.
    */
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    
    class SwingFC implements ActionListener {
    
    JTextField jtfFirst;  // holds the first file name
    JTextField jtfSecond; // holds the second file name
    
    JButton jbtnComp; // button to compare the files
    
    JLabel jlabFirst, jlabSecond; // displays prompts
    JLabel jlabResult; // displays results and error messages
    
    SwingFC() {
    
    // Create a new JFrame container.
    JFrame jfrm = new JFrame("Compare Files");
    
    // Specify FlowLayout for the layout manager.
    jfrm.setLayout(new FlowLayout());
    
    // Give the frame an initial size.
    jfrm.setSize(200, 190);
    
    // Terminate the program when the user closes the application.
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    // Create the text fields for the file names..
    jtfFirst = new JTextField(14);
    jtfSecond = new JTextField(14);
    
    // Set the action commands for the text fields.
    jtfFirst.setActionCommand("fileA");
    jtfSecond.setActionCommand("fileB");
    
    // Create the Compare button.
    JButton jbtnComp = new JButton("Compare");
    
    // Add action listener for the Compare button.
    jbtnComp.addActionListener(this);
    
    // Create the labels.
    jlabFirst = new JLabel("First file: ");
    jlabSecond = new JLabel("Second file: ");
    jlabResult = new JLabel("");
    
    // Add the components to the content pane.
    jfrm.add(jlabFirst);
    jfrm.add(jtfFirst);
    jfrm.add(jlabSecond);
    jfrm.add(jtfSecond);
    jfrm.add(jbtnComp);
    jfrm.add(jlabResult);
    
    // Display the frame.
    jfrm.setVisible(true);
    }
    
    // Compare the files when the Compare button is pressed.
    public void actionPerformed(ActionEvent ae) {
    int i=0, j=0;
    
    // First, confirm that both file names have
    // been entered.
    if(jtfFirst.getText().equals("")) {
     jlabResult.setText("First file name missing.");
     return;
    }
    if(jtfSecond.getText().equals("")) {
     jlabResult.setText("Second file name missing.");
     return;
    }
    
    // Compare files. Use try-with-resources to manage the files.
    try (FileInputStream f1 = new FileInputStream(jtfFirst.getText());
        FileInputStream f2 = new FileInputStream(jtfSecond.getText()))
    {
     // Check the contents of each file.
     do {
       i = f1.read();
       j = f2.read();
       if(i != j) break;
     } while(i != -1 && j != -1);
    
     if(i != j)
       jlabResult.setText("Files are not the same.");
     else
       jlabResult.setText("Files compare equal.");
    } catch(IOException exc) {
     jlabResult.setText("File Error");
    }
    }
    
    public static void main(String args[]) {
    // Create the frame on the event dispatching thread.
    SwingUtilities.invokeLater(new Runnable() {
     public void run() {
       new SwingFC();
     }
    });
    }
    }
    Last edited by Drew345; 08-05-2015 at 05:37 PM. Reason: add [Code]

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Could not find or load main class (half the time)?

    Drew, I gave you a link in an earlier thread. Clearly, you didn't bother to click it.
    http://www.java-forums.org/java-appl...tml#post407350

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Drew345 is offline Member
    Join Date
    May 2015
    Posts
    31
    Rep Power
    0

    Default Re: Could not find or load main class (half the time)?

    But that thread was about applets; this thread is about applications.
    That thread I had neglected to compile the applet first (javac); for this thread I am compiling properly (I think)
    The link you send was directed to "The [code] tag switches to a fixed-width (monospace) font and preserves all spacing.". I admit, I just don't see how that code tag relates to my current problem.

    I appreciate your looking this over,
    Thanks
    Drew

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Could not find or load main class (half the time)?

    ... the [code][/code] tag formats the wall of code you posted to make it better readable.

    Java Code:
    public void likeThis(){
      System.out.println("See?");
    }
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Drew345 is offline Member
    Join Date
    May 2015
    Posts
    31
    Rep Power
    0

    Default Re: Could not find or load main class (half the time)?

    Duplicate of original post deleted.
    Last edited by Drew345; 08-05-2015 at 05:29 PM.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Could not find or load main class (half the time)?

    “Error: Could not find or load main class ***.java “
    Please copy the exact text of the error message and paste it here. The *** you have inserted could be hiding a problem.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Could not find or load main class (half the time)?

    Are you sure? That .java bit in the error message is actually suspect, that is what you get when you try to run an application as java MyApplication.java instead of java MyApplication.
    Norm likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    Drew345 is offline Member
    Join Date
    May 2015
    Posts
    31
    Rep Power
    0

    Default Re: Could not find or load main class (half the time)?

    I got the forth program running (the SwingFC). The problem was that there was an Include Package statement that ran fine (was even required) in Eclipse that caused a run time error from the command line.
    So the only remaining problem is the third program, the ListDemo.java.
    The exact error is:
    Error: Could not find or load the main class ListDemo

    Other 3 are running fine now.

    EDIT -- -- --
    Nevermind. Now everything is running fine. I deleted the old classfiles for the ListDemo.class and recompiled and everything ran fine.
    I guess I'll never know what was the problem there, but at least all are running fine now.

    Thanks for all your input. In addition from learning from your responses, sometimes it helps just to formulate a clear question.

    Thanks again
    Drew
    Last edited by Drew345; 08-05-2015 at 05:42 PM.

Similar Threads

  1. Could not find or load main class
    By karim5623 in forum New To Java
    Replies: 3
    Last Post: 03-02-2015, 08:05 AM
  2. Cannot find or load or find main class
    By cornelombaard in forum New To Java
    Replies: 3
    Last Post: 11-19-2014, 03:04 PM
  3. Error: Could not find or load main class Test.class
    By java_aim in forum New To Java
    Replies: 11
    Last Post: 08-13-2013, 05:05 PM
  4. Replies: 1
    Last Post: 11-29-2012, 09:46 AM
  5. Could not find or load main class
    By kkid in forum New To Java
    Replies: 4
    Last Post: 10-15-2012, 04:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •