I know below code is accurate since it's straight out of a course.
Just get:
"Error: Main method not found in class Robot, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application"
The main method is obviously in the main method...
So why doesn't it work?
//java
/*Method parameters 1
** Robot
*/
class Robot {
public void speak(String text) {
System.out.println(text);
}
}
public class Method1 {
public static void main(String[] args) {
Robot sam = new Robot();
sam.speak("Hi I'm Sam");
}
}
Cannot run code with a method
-
- Posts: 63
- Joined: Sun Jan 07, 2018 6:30 pm
Re: Cannot run code with a method
Hi!
Thanks a lot for publishing this post. It helps others to sort out problems
The problem in your case is that you have 2 classes:
Robot and Method1
When starting a Java application you need to define the class that have the starting point (I mean the main method).
To do this you need to:
- right click on your project
- click project properties from the menu
- fill the main class name (in your case) with: Method1
- click save
I hope this helps
Thanks a lot for publishing this post. It helps others to sort out problems


The problem in your case is that you have 2 classes:
Robot and Method1
When starting a Java application you need to define the class that have the starting point (I mean the main method).
To do this you need to:
- right click on your project
- click project properties from the menu
- fill the main class name (in your case) with: Method1
- click save
I hope this helps
Re: Cannot run code with a method
In Java, each application must have the main method. The main method is the entry point of every java program. The java program searches for the main method while running. This error occurred when the main method is not available. The reason that the compiler don't complain about the public is because it doesn't care about the main method. It doesn't take difference for it if a main exists or not. It's the JVM which need a start point public and static to launch your application.
So you should make your main method as public:
public static void main(String[] args)
The JVM is programmed to look for a specific signature for the main method before executing the program. It finds this signature by looking for a specific series of bytes in the bytecode. That signature only results from compiling a method that is - public static void main. If any of the three modifiers (public, static and void) are omitted the bytecode code will look different and the JVM will not be able to find your main method.
So you should make your main method as public:
public static void main(String[] args)
The JVM is programmed to look for a specific signature for the main method before executing the program. It finds this signature by looking for a specific series of bytes in the bytecode. That signature only results from compiling a method that is - public static void main. If any of the three modifiers (public, static and void) are omitted the bytecode code will look different and the JVM will not be able to find your main method.