Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Can't find Main Class??

  1. #1
    Junior Member zlloyd1's Avatar
    Join Date
    Nov 2012
    Location
    Norfolk, Virginia
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Can't find Main Class??

    I am trying to run the following code on my computer, and it is giving me static, even though I have personally seen this exact code run on my friends computer correctly....
    On my computer it is giving me complaints about not being able to recognize the main class when I try to compile and run the code:
    errorz2.JPG
    As I said, Mark can run this code, without any changes on his laptop, and he gets a box with two buttons in it, but my computer won't even compile the code properly??
    Here is the code, in the hopes that someone can see what is going wrong for me:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    class SwingApp {
        private JButton action1;
        private JButton action2;
        private JLabel actionLabel;
        SwingApp() {
            // create JFrame container
            JFrame mainFrame = new JFrame( "Example Container Application" );
            mainFrame.setLayout(new FlowLayout());
            // set the initial size
            mainFrame.setSize( 220, 100 );
            // terminate the app when the user exits
            mainFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            // create button and label
            actionLabel = new JLabel( "This is a label that will change" );
            action1 = new JButton( "action1" );
            action2 = new JButton( "action2" );
            // add action listener for action1 using annonymous inner classes to
            // provide the event handlers when ActionEvent is fired
            action1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    actionLabel.setText("action button 1 pressed.");}});
            // same for button 2
            action2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    actionLabel.setText("action button 2 pressed.");}});
            // add components to the JFrame container
            mainFrame.add( action1 );
            mainFrame.add( action2 );
            mainFrame.add( actionLabel );
            // make our JFrame visible
            mainFrame.setVisible( true );}}
        public static void main(String[] args) {                    //THE MAIN CLASS IS RIGHT HERE!!
            SwingUtilities.invokeLater( new Runnable() {
                public void run() {
                    new SwingApp();}});}
    Can someone see where I am getting my errors here??


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Can't find Main Class??

    It would help if you formatted your code more clearly. Use one } per line to end a block, and indent it by the same amount as the line that began the block.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Can't find Main Class??

    Please post the exact error to the forums, and your code is impossible to read - I recommend formatting your code to some sort of human readable style.

  4. #4
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Can't find Main Class??

    Quote Originally Posted by copeg View Post
    some sort of human readable style.
    lmao copeg..

    Your JDE should have an auto format option.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    14
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Can't find Main Class??

    dude....its not a big mistake....
    its just misplace of '}'
    please format your code carefully and you will find it.
    if you still didn't get it.......we are here

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Can't find Main Class??

    To post readable error messages:
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member zlloyd1's Avatar
    Join Date
    Nov 2012
    Location
    Norfolk, Virginia
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Can't find Main Class??

    Thanks all, but I found my mistake.... I was trying to put this all in a java file named SwingApp.java, when it should have been Application.java. After I named the file correctly, it ran fine, with no errors. I apologize for the confusion, and sloppy formatting of my code, but problem solved, and thanks to all who responded!!

Similar Threads

  1. Cant find main class swing
    By LoganC in forum Java IDEs
    Replies: 6
    Last Post: October 18th, 2012, 09:19 PM
  2. Execution of .jar file: Could not find main class....Error'
    By suyog53 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 14th, 2012, 02:04 PM
  3. Replies: 1
    Last Post: August 12th, 2012, 11:21 AM
  4. Replies: 2
    Last Post: March 13th, 2012, 07:45 AM
  5. could not find the main class
    By Tisofa in forum Object Oriented Programming
    Replies: 1
    Last Post: September 27th, 2009, 02:58 AM

Tags for this Thread