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:Once you’ve got the compiled
String.classfile in your file system in an awkward place, that will be used by default even if you only compile theTestclass above…Basically, see if you can find a file called
String.classsomewhere.