wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Exception in thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception in thread "main" java.lang.StackOverflowError" Watch "Exception in thread "main" java.lang.StackOverflowError" New topic

Exception in thread "main" java.lang.StackOverflowError

suresh midde

Joined: Aug 22, 2008
Posts: 25
Hi ,
When I tried to execute the following code, I got an error which is presented down.

public class Test {

public static void main(String... strings ){

System.out.println("calling main 1");


It got compiled well, but while executing gave following error.

calling main 1
calling main 1
calling main 1
Exception in thread "main" java.lang.StackOverflowError
at sun.nio.cs.SingleByteEncoder.encodeArrayLoop(Unknown Source)
at sun.nio.cs.SingleByteEncoder.encodeLoop(Unknown Source)
at java.nio.charset.CharsetEncoder.encode(Unknown Source)
at sun.nio.cs.StreamEncoder.implWrite(Unknown Source)
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.BufferedWriter.flushBuffer(Unknown Source)
at java.io.PrintStream.write(Unknown Source)
at java.io.PrintStream.print(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at com.cm.portlet.trip.indent.action.Test.main(Test.java:7)
at com.cm.portlet.trip.indent.action.Test.main(Test.java:8)
at com.cm.portlet.trip.indent.action.Test.main(Test.java:8)

Since I have not defined any no-arg method for main(), I assume, it never tries to call main(). But why this was printing "calling main 1" ?

Can anyone let me understand this?

[ August 22, 2008: Message edited by: suresh midde ]
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Because you are using a varargs definition for your main, that method can take anywhere from 0 to an unlimited number of arguments. So the call to main() with no arguments satisfies that. So the method is called recursively forever, and you get the stackOverflowError. If you change the String... vararg to an array String[], then your code would not compile since you do not have a main() (with no arguments) method. You'd have to change that line to main(null). (But you would still get the StackOverflowError since you would have an infinite recursive loop - that is the main method keeps calling itself forever.)
[ August 22, 2008: Message edited by: Mark Vedder ]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 39478
I was surprised that it compiled, but it is because of your unusual use of the ... operator. You usually get that sort of Exception when you have an infinite recursion, which is exactly what you get here. If you used the conventional signature main(String[]) it wouldn't compile.

You can pass nothing to ... but not to String[].

You are calling main with no arguments, then you call main with no arguments thne you call main with no arguments then you call main with no etc etc
Joanne Neal

Joined: Aug 05, 2005
Posts: 3720
Originally posted by suresh midde:
Since I have not defined any no-arg method for main(), I assume, it never tries to call main().

You've defined your main method using the varargs syntax. That means it is expecting zero or more Strings to be passed to it. A call to main() with no parameters is therefore actually calling your defined main method., so you are recursing (calling a method from itself) - hence the stack overflow

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 39478
Great minds think alike . . . but welcome to JavaRanch
I agree. Here's the link: http://aspose.com/file-tools
subject: Exception in thread "main" java.lang.StackOverflowError