"main" as name of method causes "Target VM failed to initialize" error

I discovered a problem when I used the name main for a class method; it made the sketch not run and gave me this error:

Usage: PApplet [options] <class name> [sketch args]
See the Javadoc for PApplet for an explanation.
Could not run the sketch (Target VM failed to initialize).
For more information, read revisions.txt and Help ? Troubleshooting

Is this a bug or just me being a noob?

2 Likes

It’s you being a noob. :rofl:

Actually, you now know too much for your own good. You have discovered that Processing is cheating on you! If you pull back the curtain, you’ll find that there are a lot of details that Processing automagically deals with, so that you - or rather people even more new than you - just don’t have to worry about them.

For example… Processing’s setup() and draw() functions just… work! Right? Well, yes. But that’s because what’s really happening is they are both getting included as functions within a hidden, higher up-in-scope java class (that you normally wouldn’t even notice). And that hidden class has the main() method in it that makes them work magically.

But when you name a method main(), that method gets in that hidden main()'s way. You’re stepping on toes that you can’t even see!

For now, close the curtain and call your method something else. Might I suggest core()?

2 Likes

I guess I suspected something along those lines… Thanks a lot!

1 Like

https://en.WikiBooks.org/wiki/Computer_Programming/Hello_world#Java

class HelloWorld {
     public static void main(String[] args) {
          System.out.println("Hello, world!");
     }
}
1 Like