Why Exception in thread "main" java.lang.NoSuchMethodError: main?
Why Exception in thread "main" java.lang.NoSuchMethodError: main?
我查了一下好像是个常见的错误,但是根据我搜集到的信息,主要是main方法的行为不对造成的,而这里我什么也没检查。
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;
}
}
那么我该如何解决这个问题呢?我没有向它传递任何参数,java NBody 就失败了(我编译成功,我可以保证)。
您的 main
方法签名错误。要成为 Java 应用程序的入口点,它必须有一个 String[]
类型的参数(和 void
return 类型,并且它必须是 public 和静态):
public static void main(String[] args)
或
public static void main(String... args)
参数名称不重要。
您需要将命令行参数解析为适当的类型,例如
// 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];
public static void main(String[] args)
{
double T = Double.parseDouble(args[0]);
double dt = Double.parseDouble(args[1]);
String filename = args[2];
}
如果您使用的是命令提示符
javac NBody.java
java NBody {1stparam} {1stparam} {1stparam}
javac NBody.java
java NBody 12.2 12.3 filamename
Space这里是分隔符
12.2 will be placed to args[0]
12.3 will be placed to args[1] and
filename will be placed to args[2].
因为它是字符串,所以你将要做
double value = Double.parseDouble(string);
我查了一下好像是个常见的错误,但是根据我搜集到的信息,主要是main方法的行为不对造成的,而这里我什么也没检查。
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;
}
}
那么我该如何解决这个问题呢?我没有向它传递任何参数,java NBody 就失败了(我编译成功,我可以保证)。
您的 main
方法签名错误。要成为 Java 应用程序的入口点,它必须有一个 String[]
类型的参数(和 void
return 类型,并且它必须是 public 和静态):
public static void main(String[] args)
或
public static void main(String... args)
参数名称不重要。
您需要将命令行参数解析为适当的类型,例如
// 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];
public static void main(String[] args)
{
double T = Double.parseDouble(args[0]);
double dt = Double.parseDouble(args[1]);
String filename = args[2];
}
如果您使用的是命令提示符
javac NBody.java
java NBody {1stparam} {1stparam} {1stparam}
javac NBody.java
java NBody 12.2 12.3 filamename
Space这里是分隔符
12.2 will be placed to args[0]
12.3 will be placed to args[1] and
filename will be placed to args[2].
因为它是字符串,所以你将要做
double value = Double.parseDouble(string);