File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes cannot find main method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "cannot find main method" Watch "cannot find main method" New topic

cannot find main method

Nate McAuliffe

Joined: Sep 26, 2002
Posts: 3
I am using JBuilder 4. When I run my program, I get an error message saying that it could not find the main method. I have the main method just underneath the first few lines of global variables. Here is the code for the first part of the app. Thanks.
Dave Landers
Ranch Hand

Joined: Jul 24, 2002
Posts: 401
It's looking for a static main method.
public static void main( String[] args );
Nate McAuliffe

Joined: Sep 26, 2002
Posts: 3
If I add in the static to the main method and other methods, I get a huge number of errors about the non-static variables not being able to be referenced from a static method. Any suggestions?
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Welcome to JavaRanch!
An easy solution would be to move the instance variable declarations (the variables that are not static and are declared outside of any method - note that calling them global isn't really correct) into the main static method. Or you could declare that all of those variables are themselves static. Or you could do it the more object-oriented way, and from the main method, create an instance of the class and then use the methods (behavior) and variables (data, state) through that instance (so to speak) in order to do things.
Getting any ideas?

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Roy Wira

Joined: Sep 25, 2002
Posts: 12
Allow me to modify your code a bit
[ September 26, 2002: Message edited by: Roy Wira ]
Nate McAuliffe

Joined: Sep 26, 2002
Posts: 3
What exactly does static mean? I am used to C++ and so that is why I called the variables global. How exactly do I name the variables as static or do any thing else you mentioned?
Eric Fletcher
Ranch Hand

Joined: Oct 26, 2000
Posts: 188
Hi Nate,
The error you are getting from JBuilder is probably because you have not defined your class as the one containing the main method to run.
It's been a while and I don't have JBuilder on my machine at work, but I think it is under the "Project" menu and "Project Settings", or "Project Default Settings" , or something to that effect. In any case, a dialog will open with tabs along the top, I beleive the one you are looking for is "Run". Just find the tab that asks for the project main(If memory serves, it will have a pull-down list of all the classes in your project). Choose the one with the main you want to run.
As for static, it means that the method or variable is defined at a CLASS LEVEL, not at an instance level, i.e. it doesn't belong to a specific INSTANCE of a class, it belongs to the CLASS itself. All instances of that class will share THE SAME static entity. This means that you do not have to instantiate an object of a class to use a static variable or method, hence the reason the main method must be static; you just call it from the command line(or ide) and it will run without having to create an object of the class you are using. I'm not a C++ coder, so I'm not sure how to relate it to that arena.
Hope I haven't confused you, but try to set the project properties and see if that helps, and make sure your main method is static.

My theory of evolution is that Darwin was adopted. - Steven Wright
I agree. Here's the link:
subject: cannot find main method
jQuery in Action, 3rd edition