• Post Reply Bookmark Topic Watch Topic
  • New Topic

Main method not found even if i have declared it ?  RSS feed

 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
here is my code,
Error says : Main method not found in class talkingparrot.TalkingParrot, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Java Result: 1




[Edit - added code tags, see UseCodeTags for details]
 
Jordan Young
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RahulRise Das wrote:Hi,
here is my code,
Error says : Main method not found in class talkingparrot.TalkingParrot, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Java Result: 1









public class TalkingParrot {
int h;
boolean t;
public void hourMeth(int hour){
this.h=hour;
if((hour < 7 ) & ( hour > 20))
{
System.out.println("Hour is before 7 and after 20 and we are in trouble");
}
else
{
System.out.println("Hour is: "+hour + "And we we are not in trouble");
}
}
public void talkingMeth(boolean talking){
this.t = talking;
if(talking == true)
{
System.out.println("Parrot is talking and we are in trouble");
}
else
{
System.out.println("Parrot is silent and we are safe");
}
}
}
class ParrotTest
{
public static void main(String [] args){
TalkingParrot p = new TalkingParrot();
p.hourMeth(14);
p.talkingMeth(false);
}
}


Have you tried making a new project ? What IDE are you using?
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, i'm using the same project and i'm using netbeans 8.0.2
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd guess it's to do with how you are running the application. How are you starting it? It looks like you are running TalkingParrot instead of ParrotTest.

If you are running the project, make sure that ParrotTest is set as the class to run in the project properties. If you are trying to run the file directly, you might find it easier to move ParrotTest into its own ParrotTest.java file.
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reply to "I'd guess it's to do with how you are running the application. How are you starting it? It looks like you are running TalkingParrot instead of ParrotTest.

If you are running the project, make sure that ParrotTest is set as the class to run in the project properties. If you are trying to run the file directly, you might find it easier to move ParrotTest into its own ParrotTest.java file."

Ok, i've understood it. Thanks for the help.
 
Campbell Ritchie
Marshal
Posts: 56596
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or...

stop using NetBeans. Such a small application is easier to run from the command line/terminal. NetBeans has maybe decided to use the class you told it to use previously and you have removed a main method.
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reply to "Or...

stop using NetBeans. Such a small application is easier to run from the command line/terminal. NetBeans has maybe decided to use the class you told it to use previously and you have removed a main method."

Okay i'll use cmd, start typing in notepad++, i'll keep your advice. Thanks
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a note. You don't have to type "Reply to". You can use Quote button (which looks like this ). Give it a try.
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Just a note. You don't have to type "Reply to". You can use Quote button (which looks like this ). Give it a try.


Like this, oh nice.
 
Campbell Ritchie
Marshal
Posts: 56596
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyway, back to the original question.

Agree with Notepad++. A very good program.
Have you located the main method and got it to run?
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the compliment, yes i got my main method running but the my program was logically incorrect, i had put AND Operator instead of OR Operator, hence my program was changed to:

 
Tim Holloway
Saloon Keeper
Posts: 18800
74
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Helpful hint: Use the "Code" button on our message editor when posting structured text such as sample Java code, XML, or similar stuff. It makes it easier to read.

Offhand, what triggered my alarm button was the lack of a package name on your sample code. Especially since the error message said "Main method not found in class talkingparrot.TalkingParrot".

In other words, you should have had a "package talkingparrot;" statement at the top of your sample. And the TalkingParrot.java file should be located in the "talkingparrot" subdirectory of your source code directory. Execution from the command line would then be "java -classpath classdir talikingparrot.TalkingParrot", where "classdir is the name of the directory where you compiled your java code into. In other words, a file named "classdir/talkingparrot/TalkingParrot.class" file should exist. Or in Windows, "classdir\talkingparrot\TalkingParrot.class".
 
Campbell Ritchie
Marshal
Posts: 56596
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Avoid the | operator for boolean values. Use the shortcircuit operator || instead.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RahulRise Das,

I've added code tags for you. Take Tim's Holloway's suggestion seriously about using code tags when you posting your code.
It makes your code the way easier to read. Isn't it true?

And what Campbell Ritchie suggested to you, you can read about it in here (<- link).
Go through these tutorials, so you could discover something new.
 
Campbell Ritchie
Marshal
Posts: 56596
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also never use == true or == false.
Not if (b == true) ...
but
if (b) ...
Not if (b == false) ...
but if (!b) ...
Using == true and == false is poor style and error‑prone. What if you write = by mistake?
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I get the point. Next time I'll be more carefull. I've learned a lot in a day. Thanks everyone.
 
Campbell Ritchie
Marshal
Posts: 56596
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!