• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

cannot find main method

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic