为什么我得到"没有这样的方法例外"?

时间:2014-06-10 12:56:33

标签: java

这是我的代码,它看起来很好,但是当我编译程序时,我得到No Such方法异常

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.
    }

}

第二节课

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;
 }
 }

我得到以下异常:

"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)

使用退出代码1完成处理

我真的问过这样一个脱离背景的问题,我得到了太多的负面因素吗?

1 个答案:

答案 0 :(得分:5)

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

此错误表示您的编译器找不到您的主类。 当您遇到此类错误时,大多数情况下您将主类从文件移动到另一个文件而不更改运行配置。

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

只需右键单击" Invoyed"重新运行代码即可。 class,然后"运行Invoked.main()"。 请注意,您正在运行的班级显示在绿色运行箭头的左侧。

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