Results 1 to 9 of 9
Like Tree2Likes
  • 2 Post By ozzyman

Thread: No Main Class

  1. #1
    kobrakiller is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default No Main Class

    Hey,

    So I'm new to Java and I was looking for a tutorial to make a JFrame. Found one here: How to Make Frames (Main Windows) (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) straight from oracle. I opened the code in BlueJ, since that's what I've been learning in, and it worked fine. I tried to create a jar file out of it, but what happens is that it can't find a main class. I can manually select FrameDemo and it creates the jar but when I go to run it nothing happens. I suspect this has something to do with the main class issue.

    Code came from Oracle so I assume that the code is correct.

    Code:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*; 
    
    public class FrameDemo 
    {
        public static void main() {
            //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();
                }
            });
        }
    
        /**
         * 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);
        }
        
    }
    What am I doing wrong? Is there some way to make a standalone program from this frame?

    It's not that I need the JFrame but that I have created a small program using this JFrame as a template. Same thing, program compiles and runs great in BlueJ but jar file won't run. No error, just doesn't open.

    Any ideas?

    Thanks in advance!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: No Main Class

    Have you checked the manifest file? Do you have anything in the manifest telling which class is the main one? It will usually say something like:

    Manifest-Version: 1.0
    Main-Class: FrameDemo

    Note that if FrameDemo is in a package, the full package name should be used.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: No Main Class

    Quote Originally Posted by kobrakiller View Post
    Hey,

    what happens is that it can't find a main class. [ ... ] I suspect this has something to do with the main class issue.
    Quick thinking my dear Watson ;-) Given a bunch of classes in a .jar file, how is the JVM to know what class to use as a starting point (main class) of your application? You tell it in a manifest entry. It's explained in the Sun/Oracle tutorial section on .jar files.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    kobrakiller is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: No Main Class

    Thanks for the prompt response!
    Here's what I found in the manifest:

    Manifest-Version: 1.0
    Class-Path:
    Main-Class: FrameDemo


    Main class seems to be what it is supposed to be. Is class-path supposed to be empty?

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: No Main Class

    Quote Originally Posted by kobrakiller View Post
    ... when I go to run it ...
    How?

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

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: No Main Class

    You sure it doesnt say no main method?

    public static void main(String[] args) is the command-line method

    your method signature is:

    public static void main()

    Have a look here:
    Lesson: A Closer Look at the "Hello World!" Application (The Java™ Tutorials > Getting Started)
    DarrylBurke and Fubarable like this.

  7. #7
    kobrakiller is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: No Main Class

    To DB: When I go to run the jar file. It creates a jar file but the jar file does nothing if you try to run it. No error, nothing.

    Quote Originally Posted by ozzyman View Post
    You sure it doesnt say no main method?
    Not sure what you mean. I never get an error compiling it, the jar file has no error. In BlueJ there is a drop down for creating the jar file to choose your main class. Default option was something like "none (cannot be executed)" but I can it also had FrameDemo. I tried with both and neither one worked.

  8. #8
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: No Main Class

    You won't get a compile-time error for having no public static void main(String[] args) method, that is entirely optional for you.

    It seems like you tried to run your program though command-line like this:

    java FrameDemo

    now that will not work because that will look for the public static void main(String[] args) method, which you don't have.

    If this is your main class and you're using an IDE (such as BlueJ), when you click "Run" it will not run because the IDE will do the same thing:

    java FrameDemo

    You have to have one class in your app which executes the app, so in that case you do need a public static void main(String[] args) method somewhere.
    Last edited by ozzyman; 03-10-2012 at 06:29 PM.

  9. #9
    kobrakiller is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: No Main Class

    Got it! Thanks ozzyman and everyone for the help!

Similar Threads

  1. Replies: 0
    Last Post: 10-23-2011, 11:49 PM
  2. Could not find or load main class BubbleSort.class
    By blaqkout in forum New To Java
    Replies: 5
    Last Post: 09-12-2011, 08:54 PM
  3. Running main method class from another main class
    By tlrocketman in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 09:30 AM
  4. different multiple public class and main class
    By mr idiot in forum New To Java
    Replies: 2
    Last Post: 01-01-2009, 01:10 PM
  5. How to create main class link to another two class?
    By pearllymary78 in forum New To Java
    Replies: 6
    Last Post: 07-17-2008, 12:02 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
  •