今天在一个文件suibian.java里面写了两个classclass Person{}
class Animal{}把main 函数放在Person里面了,开发环境为Eclipse。每次运行的时候都会报错:
java.lang.NoSuchMethodError: main
Exception in thread "main" 什么原因?
class Animal{}把main 函数放在Person里面了,开发环境为Eclipse。每次运行的时候都会报错:
java.lang.NoSuchMethodError: main
Exception in thread "main" 什么原因?
文件suibian.java改成Person.java
}
public static void main(String args[]) {
System.out.println("hellos");
Test1 test = new Test1();
String ss[] = { "12", "12" };
test.main(ss);
}
}class Test1{
public static void main(String args[]) {
System.out.println("hello2");
}
}IDE下可以
事实是可以的、、
恩,改成一致没有问题。我想知道的是:是不是main函数必须写在一个跟文件名一样的class里面才行?
public static void main(String[] args) {
System.out.println("Hello "); // 打印 "Hello "
}
}class myclass_ex {
public static void main(String[] args) {
System.out.println("Hello Java ");// 打印 "Hello Java "
}}
main是一定要写在有public修饰的class类下的,像上面的两个main一般也没意义。
public static void main(String args){}
}
“毛主席教导我们说:没有实践就没有发言权”($#$#@$@#%@#$)
凡事请试一下再作结论……
package com.java.test;class Test4{
public static void main(String[] args) {
System.out.println("Hello test4");
}
}class Test3{
public static void main(String[] args) {
System.out.println("Hello test3!");
}
}以上程序放在Test2.java中,在IDE里(我这里的是MyEclipse 8.5,其它版本没试过),如果不存在与文件名同名的类,则右键菜单里不会出现Run as->Java Application选项。但是直接用该选项的热键Shift+Alt+X+J还是可以执行的(这里会弹出对话框询问运行Test3还是Test4)如果用JDK行命令,同样可以运行:
java com.java.test.Test3
Hello test3!java com.java.test.Test4
Hello test4
import java.util.*;public class WriteToProcess {
public static void main(String[] args) {
PrintStream ps = null;
try {
Process p = Runtime.getRuntime().exec("java ReadStandard");
ps = new PrintStream(p.getOutputStream());
ps.println("普通字符串");
ps.println(new WriteToProcess());
} catch(IOException e) {
e.printStackTrace();
} finally {
if(ps != null) {
ps.close();
}
}
}
}class ReadStandard {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
PrintStream ps = new PrintStream(new FileOutputStream("out.txt"));
sc.useDelimiter("\n");
while(sc.hasNext()) {
ps.println("键盘输入的内容是:" + sc.next());
}
ps.close();
}
}