[英]Why Exception in thread “main” java.lang.NoSuchMethodError: main?
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). 我没有传递任何参数给它,而Java NBody只是失败了(我编译成功,我可以保证)。
Your main
method has the wrong signature. 您的main
方法签名错误。 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): 要成为Java应用程序的入口点,它必须具有String[]
类型的单个参数(以及void
返回类型,并且必须是public和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, eg 您需要将命令行参数解析为适当的类型,例如
// 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];
}
if your are using command prompt 如果您正在使用命令提示符
javac NBody.java javac NBody.java
java NBody {1stparam} {1stparam} {1stparam} 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.