简体   繁体   中英

Is Java not installed correctly? Exception in thread “main” java.lang.NoSuchMethodError

Recently when I write any code and compile it, then try to run it I get this exception:

Exception in thread "main" java.lang.NoSuchMethodError

At first I thought there is something wrong in my code, but I couldn't find anything wrong with it. When trying to run a HelloWorld example that had worked before, if works, but if I copy the exact same code into a file HelloWorld2 I get this exception again.

The code is identical but when I used javap to decompile both class files I found a difference. In HelloWorld (the original file)

"public static void main(java.lang.String[])";

and in HelloWorld2 (the new one)

"public static void main(String[])";

without java.lang. .

I recompiled the old HelloWorld with javac and now when I try to run it it doesn't work and I get the same exception. None of my old code now works if I recompile it.

I've searched everywhere but can't find a solution to this problem - any idea what is going on here?

You may get this if you have your own class called String (without a package) in your classpath. It sounds like that's what happened. Here's a way to try to reproduce this - compile it and run it, and see if it looks the same:

class String {} 

public class Test {
    public static void main(String[] args) {
    }    
}

Once you've got the compiled String.class file in your file system in an awkward place, that will be used by default even if you only compile the Test class above...

Basically, see if you can find a file called String.class somewhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM