Results 1 to 9 of 9
Thread: No Main Class
- 03-09-2012, 04:07 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 4
- Rep Power
- 0
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:
What am I doing wrong? Is there some way to make a standalone program from this frame?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); } }
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!
-
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.
- 03-09-2012, 06:50 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Re: No Main Class
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-09-2012, 06:41 PM #4
Member
- Join Date
- Mar 2012
- Posts
- 4
- Rep Power
- 0
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?
- 03-10-2012, 12:37 AM #5
-
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)
- 03-10-2012, 05:22 PM #7
Member
- Join Date
- Mar 2012
- Posts
- 4
- Rep Power
- 0
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.
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.
-
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 05:29 PM.
- 03-13-2012, 02:12 AM #9
Member
- Join Date
- Mar 2012
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Can't Get variable calculated in another class to function in main class
By hkp30 in forum New To JavaReplies: 0Last Post: 10-23-2011, 10:49 PM -
Could not find or load main class BubbleSort.class
By blaqkout in forum New To JavaReplies: 5Last Post: 09-12-2011, 07:54 PM -
Running main method class from another main class
By tlrocketman in forum New To JavaReplies: 3Last Post: 12-06-2010, 08:30 AM -
different multiple public class and main class
By mr idiot in forum New To JavaReplies: 2Last Post: 01-01-2009, 12:10 PM -
How to create main class link to another two class?
By pearllymary78 in forum New To JavaReplies: 6Last Post: 07-16-2008, 11:02 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks