Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cannot find main method

 
Nate McAuliffe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's looking for a static main method.
public static void main( String[] args );
 
Nate McAuliffe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Roy Wira
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allow me to modify your code a bit
[/qb]<hr></blockquote>
[ September 26, 2002: Message edited by: Roy Wira ]
 
Nate McAuliffe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dirk-
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
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Cheers,
E
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic