how to fix this problem: Main method not found in class jamel.Jamel, please define the main method as:
public static void main(String[] args)
enter image description here
The main method is just a method that java calls that actually does stuff in your program. If you don't create this method then what is your program for? You'll just have a bunch of classes that you don't use.
If Jamel is your main class where your application starts from scratch then you need
public static void main(String[] args) {
}
this is the entrance of your application to start with.
from here on-words in your main method you should start calling other object.
but from the screenshot I assume that that is not your want. My guess may be wrong. but figure out what is your starting class and does it have a main method and main method calling right object to start your application.
Related
How to call JAR CLASS with CMD
I am a newcomer to java.
This is my JAR file
https://drive.google.com/file/d/16bL0n4KS9IP75tDJhhbeNLeiS3boX3iP/view?usp=sharing
I want to use CMD to call the CLASS inside JAR.
I tried to call getHardware()
java -cp uhfrcom13_v1.9.jar com.handheld.uhfr.UHFRManager.getHardware
I got the error
The main method is not found, define the main method as: public static void main(String[] args) Otherwise the JavaFX application class must extend javafx. The application class must extend javafx.application.Application
Please master teach me how to call
In UHFRManager class you have to add public static void main(String[] args) - in this method you can call your getHardware() method. When running jar file, java is always looking for main method in your class.
Make the following changes and it will work.
Make sure to have a main method (public static void main (String [] args)) in your class where getHardware is a method, then call your getHardware method to work on from the main method [you should call the class from CMD and not the specific method!!].
Make sure to use the following command, in CMD, assuming your class is UHFRManager and getHardware is a method inside it,
java -cp "uhfrcom13_v1.9.jar" com.handheld.uhfr.UHFRManager
I hope it works.
Take this code for instance
public class Hello
{
static void main(String[] args)
{
System.out.println("Hello World");
}
}
Here, I didn't write public for the main method and compiled the class. When I run the program why does the error read as "Could not find or load main class Hello.java".
My question is, if main(String[] args) is a 'method' then why say 'main class'?The point is not that public is there or not. The point is that I changed access modifier which caused main(string[] args) to be invisible to JVM. So why does JVM say main class and not main() method?
P.S. If this a stupid question then I really regret asking it.
Edit:-
Here is the error message
Error: Could not find or load main class Hello.java
There are two problems here.
main() must be declared as public static void.
However the real problem was your command line. Clearly it was
java Hello.java
It should have been
java Hello
There is no such class here as Hello.java. The name of the class is Hello.
I have just started using Javafx and I have enjoyed it so far. However I wanted to do some de-bugging with it, and I get this error.
Error: Main method not found in class fx.MyProgram, please define the main method as:
public static void main(String[] args)
If I go ahead and add a main method... the body is empty because all the magic happens from button clicks from the View. Do I need to change some configurations? Running the app works fine, but de-bugging doesn't work.
nvm I figured it out myself. For anyone else who runs into this...
public static void main(String [] args){
Application.launch(args);
}
class a {
public static void main(String args[]) {
}
}
i want to print "hello world" without touch or use above code and also without use of static and final keyword......
What are the different ways to initiate code execution.
Use public static void main(String args[]) { ... }
Use your static class initializer static { ... }. Problem: You need to get the class to load first. Solution: attempt to start the main method, but that means you need to have at least an empty main. However - OP states we cannot use 'static'.
Use an instance initializer { ... }. Problem: How do you instantiate an instance. Solution: initialize a static field with a class instance. However - OP states we cannot use static fields.
So what is left. I assume that we can use the empty main posted by the OP. From there on we build up a 4th solution:
public enum Play {
PLAY;
{ System.out.println("Hello World!"); }
public static void main(String args[]) { }
}
This should fulfill the conditions:
we do not modify the static main method, although we need the empty body just to get the class loaded.
we do not use a static initializer, we use an instance initializer instead.
to instantiate, and get the instance initializer started, we use an enum. Enum's start the initializer for each instance, meaning for each of the enum constants.
Also note the output when you try to execute a class without a main:
$ java -classpath . Play
Error: Main method not found in class Play, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
So that leaves us also the clue that we potentially can use javafx.application.Application. I did not investigate this trail any further.
You can create a JavaFX application, which is usually for displaying a GUI, and use it to output hello, world to the console and immediately shut the JavaFX GUI system down. JavaFX applications are executed by a special launch path in the java launcher. JavaFX applications do not need a main, static and final keywords.
The following snippet is just for demo purposes, I wouldn't recommend writing a JavaFX application unless you actually wish to display a GUI.
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
public class PlayApp extends Application {
#Override
public void start(Stage stage) {
System.out.println("hello, world");
Platform.exit();
}
}
// Totally useless code from the original question
// goes below the real app code and is just ignored
// and never used when the application runs.
class a {
public static void main(String args[]) {
}
}
You need to use Oracle Java 8+ to compile and execute the JavaFX application as below:
javac PlayApp.java
java PlayApp
The a class is in the PlayApp.java file only to meet the original poster's requirement from a comment:
it means the above code is fixed, you can't modify the above code but yes u can write anything to print "hello world" above or belove this code
Really, if you are writing a console application, just use a standard app format (with a static main method) instead:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
Unrelated advice on asking questions
In future, if you post such questions, I suggest that you also supply the reason for the question and why the bizarre constraints are placed on solutions. Also, address concerns such as WorldEnder's first comment on your question: "is this a code challenge? If not.. why?"
You can't just print "hello world" without MAIN method and also without using STATIC and FINAL key word in java because the main method is a function required you don't have to apply any rocket science in that thing if u are using intelliJ IDEA or any other IDE anyways this is the code
public class Basics_Of_Java {
public static void main(String[] args) {
System.out.println("Hello world");
basics of java in 1st line is the name of the java file you may have another name.
When I create a new main.java file in the default package in my Eclipse project, it generates a main method that looks like:
public static void main(String[] args)
{
}
This immediately raises a warning that says This method has a constructor name. The suggested fix is to remove the void:
public static main(String[] args)
{
}
Now rather than a warning, I get an error: Illegal modifier for the constructor in type main; only public, protected & private are permitted. If I remove the static, my code now looks like:
public main(String[] args)
{
}
This time, I still get an error, but a different one that says:
Error: Main method not found in class main, please define the main method as:
public static void main(String[] args)
Argggh! But that takes me right back to where I started. How do I define the main method so that I don't get any errors or warnings?
I'm using Eclipse Juno Service Release 2 and JavaSE-1.7. Please note, I'm very new to Java; I come from a C# background. Also, this is probably a duplicate question, but I can't find it.
Don't call your class main, but Main.
In general, stick to the Java coding standards: start class names with a capital (Main instead of main) and you won't run into these problems.
If you name the file main.java the class has to be named main too, which is against standard (classes start with a capital letter) but possible. In a class a method named the same as the class is considered a constructor. So to fix your problem and fit the standard rename your class and the file to Main with a capital "M"
Change the name of your class from main to Main or to something else. Also, following the JavaBean API specification, your classes must be in CamelCase with the first letter in capital.
Not 100% related with question, but you should also do not create classes with the names of Java JDK classes, for example String:
public class String {
public static void main(String[] args) {
System.out.println("Try to execute this program!");
}
}
This won't only give problems to compiler/JVM but also for future readers (remember that you are a future reader of your own code as well).
Note: to fix the code above, just refer to java.lang.String class using its full name:
public class String {
public static void main(java.lang.String[] args) {
System.out.println("Try to execute this program!");
}
}
Or even better, change the name of the class.
In java, the class name and file name must match. If you have a file named main.java, then the class name has to be Main too, and in that case, the constructor method would be named main, so you couldn't have a main method.
Change your file and class name to something other than main.