aspose file tools*
The moose likes Beginning Java and the fly likes what did i break? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what did i break? "no main method found"" Watch "what did i break? "no main method found"" New topic
Author

what did i break? "no main method found"

Steven Hofmann
Greenhorn

Joined: Oct 24, 2010
Posts: 17
So I'm trying to finish up some stuff here on the last day of classes. I have a couple last programs to write and am running into some problems. I wrote a program that needs a constructor, accessor, and mutator methods, and it basically takes in a couple strings and doubles and simply returns a value.

I don't know what I did, but all of a sudden on line #5, netbeans is saying that "class main is public, should be declared in a file named main.java"... Well, I'm in the file main.java. I don't get it, it starts off the same as all my other programs. Additionally, when I try to run it, it says it has no main method. Maybe you guys can see something that I am not?



I'm pretty sure it's something dumb.



Thanks!



Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
The main method must be static.
Steven Hofmann
Greenhorn

Joined: Oct 24, 2010
Posts: 17
if i made it static, netbeans underlines line 15 and states: "non-static variable this cannot be referenced from a static content", which is why i removed static from it.


EDIT: nope, i was thinking about the wrong program, my bad. no, for some reason it still says no main method found. adding static makes line 15 not work and the program error out.
Steven Hofmann
Greenhorn

Joined: Oct 24, 2010
Posts: 17
updated
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Not line 15 - line 7. We're talking about the main method.

And it should go without saying that it's confusing -if not problematic- to have a class named X that contains a method named X.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

"My carburetor was making a ticking sound so I removed it. The ticking sound is gone, but now the car won't start."

Solve the real problem rather than "solving" problems by patching symptoms. What is the exact error message you receive and on what line?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Steven Hofmann
Greenhorn

Joined: Oct 24, 2010
Posts: 17
Bear Bibeault wrote:"My carburetor was making a ticking sound so I removed it. The ticking sound is gone, but now the car won't start."

Solve the real problem rather than "solving" problems by patching symptoms. What is the exact error message you receive and on what line?



the problem is exactly as in my first post: i try to run the program, and it says "no main method found".
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Even after you put the static keyword back in?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Steven Hofmann wrote:if i made it static, netbeans underlines line 15 and states: "non-static variable this cannot be referenced from a static content", which is why i removed static from it.


Generally, it is not a good idea to make a change without knowing why it was an error. And making a change to make the compiler not complain is rarely a good idea. For example, I can pull the battery from my car to get my car's computer to stop turning on the "check engine" light, but I don't think that would be a good idea...

[EDIT: Bear beat me to the example, and he used a vehicle example to boot]

Henry



Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Steven Hofmann
Greenhorn

Joined: Oct 24, 2010
Posts: 17
Bear Bibeault wrote:Even after you put the static keyword back in?


Yes, it still says no main method found, and it doesn't run the program. I'm really scratching my head on this, as I have maybe 15 programs from the semester that start the same way.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

At this point you'll have to post the code.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Steven Hofmann wrote:
I don't know what I did, but all of a sudden on line #5, netbeans is saying that "class main is public, should be declared in a file named main.java"... Well, I'm in the file main.java. I don't get it, it starts off the same as all my other programs. Additionally, when I try to run it, it says it has no main method. Maybe you guys can see something that I am not?


Double check that the class name matches the file name -- remember that Java is case sensitive. Double check that the package name matches the directory structure too.

Unfortunately, I don't know anything about netbeans, so can't tell if you corrupted something with the IDE specific files.

Henry
Alan Hampson
Ranch Hand

Joined: Apr 10, 2009
Posts: 31
You have two issues here. First, as was already mentioned, you have to have the static keyword on your main method. The two methods

and


are two different methods because they have different signatures. If you don't have the static in the method signature, the classloader can't find main when it tries to run your program.

When you put static back in, you do get an error on line 15


This is because your class, Stock isn't static and is nested in the main class. In order to use it in the static main, Stock must be static as well. If you add the static keyword to the Stock class, your code will build and run. At least it did for me.


I would also agree with others here that you probably shouldn't name your class main. This is bad form and shouldn't become a habit when you are just learning java.

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Maybe more importantly, why is Stock an inner class of main? It doesn't need to be, and it rather looks like this was one more patch repair after the IDE complained about it being a public class in main.java.

Also, by convention Java class names start with an uppercase letter.
http://www.oracle.com/technetwork/java/codeconvtoc-136057.html

And I second Lester's recommendation to not have a class with a method of the same name. At the very least, there should be a difference of case. But a class should always have a meaningful name. Main isn't.


luck, db
There are no new questions, but there may be new answers.
Alan Hampson
Ranch Hand

Joined: Apr 10, 2009
Posts: 31
Maybe more importantly, why is Stock an inner class of main? It doesn't need to be, and it rather looks like this was one more patch repair after the IDE complained about it being a public class in main.java.


Yup. Well I wasn't even going to go there!

You are correct though. It should be a separate class in it's own file.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: what did i break? "no main method found"