1

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?

configurator
  • 38,246
  • 13
  • 76
  • 112
Ahmad
  • 115
  • 7
  • 5
    I'm out of breath just reading that. – robert_x44 Jan 06 '11 at 23:44
  • This Community Wiki question lists the possible causes of this common problem: http://stackoverflow.com/questions/5407250/causes-of-java-lang-nosuchmethoderror-main-exception-in-thread-main – Stephen C Jun 28 '11 at 14:41

1 Answers1

3

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.

Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929