Java 为什么我得到“没有这样的方法异常”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24141578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:26:46  来源:igfitidea点击:

Why do I get "no such method exception"?

java

提问by Israel Cohen

This is my code and it seems to be fine but when I compile the program I get No Such method exception

这是我的代码,它似乎很好,但是当我编译程序时,我得到 No such method 异常

import java.io.IOException;

public class Invoked {

 public static String celebname = "Sometext" ;
 public static  String urladdress = "someurl
public static void main(String args[])  {

Main.setpagesource(celebname);
Main.seturl(urladdress);

    try {
        Main.Calculate();
    } catch (IOException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
    //To change body of catch statement use File | Settings | File Templates.
    }

}

And the second class

和第二班

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Main {


public static String address;
public static String celebfilename;
public static String pagesource;

public static void Calculate() throws MalformedURLException {

      URL url1 = new URL(address) ;
    URLConnection connection1 = null;
    try {
        connection1 = url1.openConnection();
    } catch (IOException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
    BufferedReader br = null;
    try {
        br = new BufferedReader(
                new InputStreamReader(connection1.getInputStream()));
    } catch (IOException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
    String fileName = "C:\Users\Dell\Documents\"+"pagesource"+".txt";
    File file = new File(fileName);

    if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    FileWriter fw = null;
    try {
        fw = new FileWriter(fileName);
    } catch (IOException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
    BufferedWriter bw = new BufferedWriter(fw);

    String textreader;
    try {
        while ((textreader = br.readLine()) != null) {
            bw.write(textreader);
        }
    } catch (IOException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }


 }

 public static void seturl(String addressname){

   address = addressname;
}

 public static void settextfilename(String celebfilename1){
   celebfilename = celebfilename1;
}


 public static void setpagesource(String pagesourcename){
    pagesource = pagesourcename;
 }
 }

I get the following exception:

我收到以下异常:

"C:\Program Files\Java\jdk1.7.0_07\bin\java" -Didea.launcher.port=7541 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 12.0.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_07\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_07\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_07\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_07\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_07\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_07\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_07\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_07\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_07\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_07\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_07\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_07\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_07\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_07\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_07\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_07\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_07\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_07\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_07\jre\lib\ext\zipfs.jar;C:\Users\Dell\IdeaProjects\untitled6\out\production\untitled6;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 12.0.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain Main


Exception in thread "main" java.lang.NoSuchMethodException: Main.main([Ljava.lang.String;)
at java.lang.Class.getMethod(Class.java:1622)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:113)

Process finished with exit code 1

进程以退出代码 1 结束

Do I really have asked such an out of context question that I have gotten those too many negative points?

我真的问了这样一个断章取义的问题,以至于我得到了太多的负面评价吗?

回答by gwenzek

Exception in thread "main" java.lang.NoSuchMethodException: Main.main([Ljava.lang.String;)

This error means your compiler didn't find your main class. When you have this kind of error, most of the time it meanss you moved your main class from a file to another without changing your run configuration.

这个错误意味着你的编译器没有找到你的主类。当您遇到此类错误时,大多数情况下这意味着您将主类从一个文件移动到另一个文件,而无需更改运行配置。

In Intellij, the first time you run your program you right-click on a file containing a "public static main(String[] args)" method. The next time you click on "run" it use the same file and look for a main class. If you moved your main class, the IDE can't guess it and therefore throw you this exception.

在 Intellij 中,第一次运行程序时,右键单击包含“public static main(String[] args)”方法的文件。下次单击“运行”时,它会使用相同的文件并查找主类。如果您移动了主类,IDE 无法猜测它,因此会向您抛出此异常。

So just rerun your code with right-click on the "Invoyed" class, then "run Invoked.main()". Note that the class you are running is displayed left of the green run arrow.

因此,只需右键单击“Invoyed”类,然后“运行 Invoked.main()”即可重新运行您的代码。请注意,您正在运行的课程显示在绿色运行箭头的左侧。

As a general tip, if you got an error first check you are running the correct class and not some old main class.

作为一般提示,如果您遇到错误,请首先检查您运行的是正确的类,而不是一些旧的主类。