Java main()方法的方法签名是:
public static void main(String[] args){
...
}
Run Code Online (Sandbox Code Playgroud)
这种方法是否有理由是静态的?
编写java程序需要主要方法吗?
这是我的代码:
package example;
public class HelloWorld {
public HelloWorld() {
}
public String getHelloWorld() {
return "Hello From Java!";
}
}
Run Code Online (Sandbox Code Playgroud)
它在编译时显示错误:
java.lang.NoSuchMethodError: main
Exception in thread "main"
Run Code Online (Sandbox Code Playgroud) 对不起,我是Java新手,我的代码有问题.我已经阅读了线程,并看到了很多关于这个特定错误的例子(java.lang.NoSuchMethodError: main Exception in thread "main").我似乎无法绕过我要添加(static void main(String[] args))代码的地方.如果你们能指出我正确的方向,我会非常感激.
这是我有的:
public class Employee {
String name;
String department;
double hourlyRate;
Employee(String name, String department, double hourlyRate) {
this.name = name;
this.department = department;
this.hourlyRate = hourlyRate;
}
public void setDepartment(String department) {
this.department = department;
}
public void setHourlyRate(double hourlyRate) {
this.hourlyRate = hourlyRate;
}
public String getNameAndDepartment() {
return name + " " + department;
}
double weeklyPay(int numOfHourWorked) {
if (numOfHourWorked < 40) { …Run Code Online (Sandbox Code Playgroud) 我正在使用Eclipse.我删除了所有内容并离开了主要功能 - 没有任何工作.有人可以帮忙吗?
package good;
import java.io.*;
public class FiFo {
public static void main()
{
System.out.println("here");
}
}
class FileReader {
public FileReader(String fileName)
{
try {
FileInputStream fstream = new FileInputStream(fileName);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud) 我收到了上述消息.代码如下:
class Test
{
public static void main(String ar[])
{
printf("hai");
}
}
Run Code Online (Sandbox Code Playgroud)
这个问题是如何引起的?我该如何解决?
我正在为我的课程学习Java,而且我遇到了一堵砖墙.我的任务是开发一个简单的命令行程序.为了简化操作,我获得了以下示例代码进行修改,因此我不必从头开始.
package assignment;
public class Main {
private final static String[] mainMenuOpts = {"Students","Lecturers","Admin","Exit"};
private final static String[] studentMenuOpts = {"Add Student","List all Students","Find a Student","Return to Main Menu"};
private Menu mainMenu = new Menu("MAIN MENU",mainMenuOpts);
private Menu studentMenu = new Menu("STUDENT MENU",studentMenuOpts);
private DataStore data = new DataStore();
private java.io.PrintStream out = System.out;
private ReadKb reader = new ReadKb();
/** Creates a new instance of Main */
public Main() {
run();
}
private void run(){
int ret = mainMenu.display(); …Run Code Online (Sandbox Code Playgroud) 我不明白为什么这个消息来--------->
java.lang.NoSuchMethodError: main
Exception in thread "main" .
Run Code Online (Sandbox Code Playgroud)
我知道它期待main()方法但是我正在构建一个不包含main方法的applet而是包含init()方法.所以我该怎么做?我的代码是s遵循--->
import java.applet.*;
import java.awt.*;
public class Ballbewegung1 extends Applet implements Runnable
{
// Initialisierung der Variablen
int x_pos = 10; // x - Position des Balles
int y_pos = 100; // y - Position des Balles
int radius = 20; // Radius des Balles
public void init()
{
setBackground (Color.blue);
}
public void start ()
{
// Schaffen eines neuen Threads, in dem das Spiel l?uft
Thread th = new Thread …Run Code Online (Sandbox Code Playgroud) 我收到以下错误:
java.lang.NoSuchMethodError:线程"main"中的主要异常
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class SwimCalc extends JFrame implements ActionListener {
private JTabbedPane jtabbedPane;
private JPanel Customers;
JTextArea NameTextCustomers, ExistTextCustomers, NameTextContractors,
ExistTextContractors;
public SwimCalc() {
setTitle("Volume Calculator");
setSize(300, 200);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
getContentPane().add(topPanel);
createCustomers();
jtabbedPane = new JTabbedPane();
jtabbedPane.addTab("Customers", Customers);
topPanel.add(jtabbedPane, BorderLayout.CENTER);
}
/* CREATE CUSTOMERS */
public JPanel createCustomers() {
Customers = new JPanel();
Customers.setLayout(null);
NameTextCustomers = new JTextArea();
NameTextCustomers.setBounds(10, 10, 350, 150);
NameTextCustomers.setLineWrap(true); …Run Code Online (Sandbox Code Playgroud) 我已经读过这个帖子,因为它说我必须有public static void main(java.lang.String[] args)
我的主要功能是这样的:
public static void main(String[] args) throws FileNotFoundException, IOException, TrieException, TSException {
CSVReader reader=new CSVReader(new FileReader(".//Data//test1.csv"));
String[] nextline;
int linenumber=0;
double[] numbers=new double[10];
double[] times=new double[10];
for(int i=0;i<=7;i++)
{
nextline = reader.readNext();
numbers[i]= Double.parseDouble(nextline[0]);
times[i]=i;
}
DiscordsAndMotifs dr= edu.hawaii.jmotif.sax.SAXFactory.series2DiscordsAndMotifs(numbers, 4, 2, 2, 2,null);// If I comment this line of code, my programm works without any error
}
Run Code Online (Sandbox Code Playgroud)
如果我运行我的程序,我会收到此错误:
Exception in thread "main" java.lang.NoSuchMethodError: org.hackystat.utilities.logger.HackystatLogger.getLogger(Ljava/lang/String;Ljava/lang/String;) Ljava/util/logging/Logger;
at edu.hawaii.jmotif.sax.SAXFactory.<clinit>(SAXFactory.java:51)
at motif.discovery.MotifDiscovery.main(MotifDiscovery.java:35)`
Run Code Online (Sandbox Code Playgroud)
我的项目中需要库.这个错误的原因是什么?你能帮我解决这个问题吗?
我有下面这个问题
package com;
import java.io.PrintStream;
import java.net.URL;
import java.net.URLConnection;
public class Caller
{
public static void main(String[] args)
{
try
{
URL url = new URL("http://localhost:8080/Test/Ravi.jsp");
connection = url.openConnection();
}
catch (Exception ioe)
{
URLConnection connection;
System.err.println("IOException: " + ioe.getMessage());
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我跑步时,我正在接受
C:\ Program Files\Apache Software Foundation\apache-tomcat-6.0.33\webapps\Test\WEB-INF\classes> java com.Caller
Exception in thread "main" java.lang.NoClassDefFoundError: com/Caller
Caused by: java.lang.ClassNotFoundException: com.Caller
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could …Run Code Online (Sandbox Code Playgroud) 我正在通过终端在OS X上学习Java.当我编译下面的代码时,我没有得到任何错误但是当我尝试运行它时,我得到了
线程"main"中的异常java.lang.NoSuchMethodError:main
我的代码:
public class Problem5{
public void main(String[] args) {
int n = 1;
while(!checkMod(n)){
n++;
}
}
public boolean checkMod(int in)
{
int count = 0;
for(int i=1; i<20; i++)
{
if(in%i == 0)
{
count = count + 1;
}
}
if(count ==19)
{
return true;
}
else{
return false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我从main方法中删除了"static",因为我调用了checkMod方法.
我该如何正确编码?
谢谢
public class m
{
int a; //class variable
void f1()
{
int b=10;
System.out.println(a);
System.out.println(b);
}
}
class B
{
public static void main(String args[])
{
m ob=new m(); //object created
ob.f1(); //calling f1 method
}
}
Run Code Online (Sandbox Code Playgroud) import java.io.*;
import java.lang.Math;
class Squr
{
public static void main ()
{
int m =10,n;
double z = 10.4,p;
Squr square = new Squr();
p = (double)square.mysqrt(z);
n = (int)square.mysqrt(m);
System.out.println ("square root of 10 : " + n );
System.out.println ("square root of 10.4 : "+ p );
}
double mysqrt (double y)
{
return Math.sqrt(y);
}
int mysqrt (int x)
{
return (int)Math.sqrt(x);
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码正在编译,但是当我们尝试执行它时,它给出了"线程中的异常"主"Java.lang.NoSuchMethodError:main"