Why Exception in thread "main" java.lang.NoSuchMethodError: main?

534 views Asked by At

Well as I researched it seems to be a common error, but through the information I collected, it is mainly caused by the misbehaviour of main method, while in here I inspected nothing.

import java.util.Scanner;

public class NBody {

    public static void main(double T, double dt, String filename) {

        In f = new In(filename);
        int N = f.readInt();
        double R = f.readDouble();

        Planet planets[] = new Planet[N]; 

        for (int i = 0; i < N; i++) {
            planets[i] = getPlanet(f);
        }

        StdDraw.setScale(-R, R);

        for (Planet star:planets) {
            star.draw();
        }
    /*
        for (Planet star:planets) {
            star.update(dt);
            StdDraw.picture(star.x, star.y, star.imgName);
        }
    */

    }

    public static Planet getPlanet(In file) {
        double x = file.readDouble();
        double y = file.readDouble();
        double xVelocity = file.readDouble();
        double yVelocity = file.readDouble();
        double mass = file.readDouble();
        String imgName = file.readString();
        Planet p = new Planet(x, y, xVelocity, yVelocity, mass, imgName);
        return p;
    }

}

So how can I solve this problem? I didn't transfer any arguments to it and the java NBody just fails (I compiled successfully, I could assure that).

2

There are 2 answers

6
Jon Skeet On BEST ANSWER

Your main method has the wrong signature. To be an entry point for a Java application, it must have a single parameter of type String[] (and a void return type, and it must be public and static):

public static void main(String[] args)

or

public static void main(String... args)

The parameter name is unimportant.

You'll need to parse the command line arguments into appropriate types, e.g.

// TODO: Validation, e.g. that there are 3 command line arguments
// TODO: Use meaningful and conventional variable names
double T = Double.parseDouble(args[0]);
double dt = Double.parseDouble(args[1]);
String filename = args[2];
0
Aradhya On
public static void main(String[] args)
{
double T = Double.parseDouble(args[0]);
double dt = Double.parseDouble(args[1]);
String filename = args[2];
}

if your are using command prompt

javac NBody.java

java NBody {1stparam} {1stparam} {1stparam}

javac NBody.java 
java NBody 12.2 12.3 filamename

Space is the delimiter here

12.2 will be placed to args[0] 
12.3 will be placed to args[1] and 
filename will be placed to args[2].

since it is String you will be doing

  double value = Double.parseDouble(string);