4

How can I configure the default java stack trace printing behaviour so only the head is visible? Currently I use 2>&1 | head or | less -i and I find it suboptimal.

When having an uncaught exception, Java prints me 115 lines and then the text ... 102 more. 99% to 97% of this trace is irrelevant to me. This leaves the first 3 lines interesting.

How can I configure Java to print me 2 lines and then, in my example case, ... 215 more?

n611x007
  • 8,952
  • 8
  • 59
  • 102
  • you could convert your stack trace to a string ([this answer](http://stackoverflow.com/questions/1149703/how-can-i-convert-a-stack-trace-to-a-string) might help) and take a substring of this string, but I have this feeling this is not optimal either... – Mr Tsjolder from codidact Apr 30 '15 at 06:20
  • As [this SO question](http://stackoverflow.com/questions/21706722/fetch-only-first-n-lines-of-a-stack-trace) pointed out, you want to get the array for the stack trace, and then subset that for the rows you want to see. – Tim Biegeleisen Apr 30 '15 at 06:21
  • You wrote `uncaught exception`. I guess you have no control over the statcktrace at that point. Am I right? – SubOptimal Apr 30 '15 at 06:23
  • @SubOptimal yes you are right – n611x007 Apr 30 '15 at 06:23
  • 1
    @naxa Do you want to change Java's default stack printing behavior throughout your entire application, or only in area? – Tim Biegeleisen Apr 30 '15 at 06:28
  • @TimBiegeleisen for my entire application would be good for me currently. (That said I can imagine future readers looking for the other one too...) – n611x007 Apr 30 '15 at 06:40

1 Answers1

5

You can specify JVM parameter MaxJavaStackTraceDepth:

-XX:MaxJavaStackTraceDepth=2

You can set this parameter in your run script. If you use IDE, there's a VM options or VM arguments field in a run configuration dialog. You can set the above parameter in the field.

For more detail, please refer to http://stas-blogspot.blogspot.kr/2011/07/most-complete-list-of-xx-options-for.html#MaxJavaStackTraceDepth

ntalbs
  • 28,700
  • 8
  • 66
  • 83