Exception in thread "main" java.lang.NoSuchMethodError: main [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Exception in thread “main” java.lang.NoSuchMethodError: main
I got the above message. The code is as follows:
class Test
{
public static void main(String ar[])
{
printf("hai");
}
}
How is this problem caused and how can I fix it?

The class which you're trying to execute doesn't have a main method.
Since your main method looks syntactically fine, this can have two causes:
You're executing the wrong class.
The actual class file doesn't contain this code.
The solution is obvious:
Make sure that your command is pointing the correct class file, you might have multiple class files with the same name and be sitting in the wrong directory.
Make sure that you've compiled the correct source file into the correct class file before, you might have edited one and other and forgot to recompile.

In addition to the problem that's causing the current exception (see BalusC's answer), the proper "Hello World" in Java is:
class Test
{
public static void main(String[] args) {
System.out.println("hai");
}
}
See: java.lang.System

I see your problem, the signature is not correct.
It should be public static void main(String[] args)

It could also be a class path issue which causes Eclipse to get confused and not able to find your class when it tries to run it. I would look at the Java Build Path in the Project Properties to make sure there are no errors.

Related

Java application start point [duplicate]

This question already has answers here:
Entry point for Java applications: main(), init(), or run()?
(5 answers)
Closed 4 years ago.
I downloaded a simple java app and I want to know which file I need to run to start. There are several classes in the project and I do not know which one is the main one. Thank you.
Search for the class with the main method, that is the method where program execution starts. It looks like
public static void main(String[] args) { ... }
It's inside the class Main.java of your project:
public class Main extends Application {
public static void main(final String[] args) {
launch(args);
}
// [...]
}
Open the project in an IDE and run this class. Or do it manually in the console like:
javac Main.java
java Main
Only .jar files can automatically be started like .exe files. Your project only contains code and you will need to compile and run it on your own.
You should have public static void method called "main" taking array of String as parameter in one of the classes. Something like below
public static void main(String... args) {
System.out.println("test");
}
This is your entry point.
#Edit
OP added code so here is an answer update:
Your entry point is in class Main and method is
public static void main(final String[] args
If you're using intellij just run click on this method and select Run "Main.main()"

basic java programming (naming a file)

i got a run-time error on executing a file 'complement.java' that read
D:\java\files\java complement
Error:Could not find or load main class complement
the class structure follows as below
import java.io.*;
class billion
{
.
.
.
//class definition
}
what could be the cause for the prompt mentioned above?
First of all, this question can be easily answered by 5 seconds of quick googling, supposing you dont know java.
Anyway, the error only shows when your main class is missing a main method, which start the whole program. The main method is
public static void main(String[] args) {}
Add this to your main class, and start it again.
File name and class name need to be same. And the class you mention in java command should have a main method.
public static void main(String[] args) {}

Can't we run main method inside enum? [duplicate]

This question already has answers here:
Java can't find main class
(5 answers)
Closed 6 years ago.
I had seen a video where, main() can be run with in an enum.
I am trying to do the same but it's not working.
Here is my code
public enum EnumMain {
ABC, XYZ;
public static void main(String[] args) {
System.out.print("MIAN");
}
}
Output
(The code compiles fine)
Error: Could not find or load main class EnumMain
I think this has something to do with the Java version, may be in Java 8, they are not allowing to run main() method from enum anymore.
PS I am compiling and running the file from windows command prompt.
Note If I change the enum to class then it runs fine (I don't have any classpath issues)
You can run main within enum.
public enum TestEnumMain{
val1, val2;
public static void main(String[] args)
{
System.out.println("Hello");
}
}
Problem would be with you your path variables. Make sure they are configured right. Refer this thread for setting path variables propertly.
No, I have run your code on Java 8 and it is perfect. It runs to give output MIAN. I think the problem lies in classpath or in your IDE. You might try after cleaning the project.

Getting NoSuchMethodError at runtime

I have looked through many answers to similar questions. But couldn't narrow down to a solution.
Following is the code: (Simplifying names for readability)
First class:
package p1;
public class C1 {
public static void test() {
System.out.println("Boom!");
}
}
Second class:
package p2;
import p1;
public class C2 {
public static void main(String[] params) {
C1.test();
}
}
Clean-Build doesn't give any error. (No compilation error)
But at runtime I'm getting following error:
Exception in thread "main" java.lang.NoSuchMethodError: C1.test()V
at C2.main(C2.java:6)
Java Result: 1
P.S. I'm using Netbeans.
This means that you are running your class C2 with an old version of class C1 in the classpath (a version that did not yet have the test() method).
Make sure you don't have old versions of C1.class somewhere. Remove all your *.class files and recompile everything, and then try to run it again.
Addition: As Kevin Bowersox noted in a comment, your main method must look like this:
public static void main(String[] args)
It must take a String[] as an argument.
It will properly compile and run only if main function will have String tab as args.
But also check versions of class C1 and C2, try rebuild your project to recompile that classes.
public static void main(String args[]) {
C1.test();
}
i think you should import it as
import p1.*;
Than you will get access to all classes and member functions in it.
Netbeans sometimes likes to get stuck after some changes and clean build doesn't work then.
Try editing each file that has been recently modified and saving it again (e.g. put a whitespace in a random place). After that, clean and build the project again.
If my memory refreshes and as Jesper pointed out, I also encountered that same issue NoSuchMethodFoundException under that same scenario (having still old class references that have not been cleaned).
I just copied your code snippets with 2 different packages directly in to my netbean, compiled and runned C2. It did print the BOOM! message.
In my case using :
public static void main(String args[]){
}
does not make a difference when I compiled and runned the code.
public static void main(String params[]){
}
It makes sense since the main class should have the correct method signature of main.
Here args or params, should not make a huge difference, I believe; as what we have inside the method is simply a reference for the inner body of the method that it uses.
Still definitely it is good practice to follow the standard signature for main.
I would recommend to clean the project and copy the contents from scratch in a new project and build it again, sometimes netbeans can go crazy.

Is Java not installed correctly? Exception in thread "main" java.lang.NoSuchMethodError

Recently when I write any code and compile it, then try to run it I get this exception:
Exception in thread "main" java.lang.NoSuchMethodError
At first I thought there is something wrong in my code, but I couldn't find anything wrong with it. When trying to run a HelloWorld example that had worked before, if works, but if I copy the exact same code into a file HelloWorld2 I get this exception again.
The code is identical but when I used javap to decompile both class files I found a difference. In HelloWorld (the original file)
"public static void main(java.lang.String[])";
and in HelloWorld2 (the new one)
"public static void main(String[])";
without java.lang..
I recompiled the old HelloWorld with javac and now when I try to run it it doesn't work and I get the same exception. None of my old code now works if I recompile it.
I've searched everywhere but can't find a solution to this problem - any idea what is going on here?
You may get this if you have your own class called String (without a package) in your classpath. It sounds like that's what happened. Here's a way to try to reproduce this - compile it and run it, and see if it looks the same:
class String {}
public class Test {
public static void main(String[] args) {
}
}
Once you've got the compiled String.class file in your file system in an awkward place, that will be used by default even if you only compile the Test class above...
Basically, see if you can find a file called String.class somewhere.

Categories

Resources