在Java中,如何获取完整的堆栈跟踪?

huangapple go评论76阅读模式
英文:

In Java - how do I get a full stack trace

问题

目前我从代码运行中获得以下输出:

由于: java.lang.NullPointerException
	at com.gargoylesoftware.htmlunit.html.HtmlScript$2.execute(HtmlScript.java:227)
	at com.gargoylesoftware.htmlunit.html.HtmlScript.onAllChildrenAddedToPage(HtmlScript.java:256)
	at com.gargoylesoftware.htmlunit.html.parser.neko.HtmlUnitNekoDOMBuilder.endElement(HtmlUnitNekoDOMBuilder.java:560)
	at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
	at com.gargoylesoftware.htmlunit.html.parser.neko.HtmlUnitNekoDOMBuilder.endElement(HtmlUnitNekoDOMBuilder.java:514)
	...(省略部分输出)...

上面的 "... 30 more" 意味着我不知道导致问题的代码行。如何获取完整的堆栈跟踪呢?

另外,如果似乎没有运行我的 catch 语句中的 printStackTrace,但如果我使用以下命令关闭日志记录:

java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF);

java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF);

我已按照下面的建议放入了我的完整代码。没有放入实际的 URL,但是放入了以下内容:

import com.gargoylesoftware.htmlunit.html.*;
import java.io.File;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class download_to_send_to_stackoverflow
{
     public static void main(String args[])
     {
      String url = "不想包括实际的网址 ";
      HtmlPage page = null;
      WebClient webClient = new WebClient();
	  webClient.getOptions().setRedirectEnabled(true);  
	  webClient.getOptions().setJavaScriptEnabled(true);  // 尝试将其设置为 false - 但未起作用
 	  webClient.getOptions().setCssEnabled(false);
	  webClient.getOptions().setUseInsecureSSL(true);
          webClient.getOptions().setTimeout(30000);
	  webClient.setJavaScriptTimeout(30000); // 例如 50 秒
 	  webClient.waitForBackgroundJavaScript(15000);   // 添加于 2017/1/24
	  webClient.waitForBackgroundJavaScriptStartingBefore(30000);   // 添加于 2017/1/24
          try
          {
               page = webClient.getPage(url);
   	       savePage_just_html(page);  
	       }
          catch( Exception e )
          {
               System.out.println( "异常抛出:----------------------------------------" + e.getMessage() );
               e.printStackTrace();
               }
     }
     protected static String savePage_just_html(HtmlPage page)
     {
          try
          {
          	File saveFolder = new File("spool/_");
	        page.save(saveFolder);   // 这是导致带有有限堆栈跟踪错误的行
          }
          catch ( Exception e )
          {
               System.out.println( "IOException was thrown: ---------------------------------- " + e.getMessage() );
               e.printStackTrace();
          }
          return "file.html";
     }
}
page.save(saveFolder);   上面是问题行如果我删除它我就不会得到有限的堆栈跟踪错误但我也不会保存 HTML 页面而我希望这样做问题是为什么这行只打印有限的堆栈跟踪

<details>
<summary>英文:</summary>

Currently I get the following output from code run

Caused by: java.lang.NullPointerException
at com.gargoylesoftware.htmlunit.html.HtmlScript$2.execute(HtmlScript.java:227)
at com.gargoylesoftware.htmlunit.html.HtmlScript.onAllChildrenAddedToPage(HtmlScript.java:256)
at com.gargoylesoftware.htmlunit.html.parser.neko.HtmlUnitNekoDOMBuilder.endElement(HtmlUnitNekoDOMBuilder.java:560)
at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
at com.gargoylesoftware.htmlunit.html.parser.neko.HtmlUnitNekoDOMBuilder.endElement(HtmlUnitNekoDOMBuilder.java:514)
at net.sourceforge.htmlunit.cyberneko.HTMLTagBalancer.callEndElement(HTMLTagBalancer.java:1192)
at net.sourceforge.htmlunit.cyberneko.HTMLTagBalancer.endElement(HTMLTagBalancer.java:1132)
at net.sourceforge.htmlunit.cyberneko.filters.DefaultFilter.endElement(DefaultFilter.java:219)
at net.sourceforge.htmlunit.cyberneko.filters.NamespaceBinder.endElement(NamespaceBinder.java:312)
at net.sourceforge.htmlunit.cyberneko.HTMLScanner$ContentScanner.scanEndElement(HTMLScanner.java:3189)
at net.sourceforge.htmlunit.cyberneko.HTMLScanner$ContentScanner.scan(HTMLScanner.java:2114)
at net.sourceforge.htmlunit.cyberneko.HTMLScanner.scanDocument(HTMLScanner.java:937)
at net.sourceforge.htmlunit.cyberneko.HTMLConfiguration.parse(HTMLConfiguration.java:443)
at net.sourceforge.htmlunit.cyberneko.HTMLConfiguration.parse(HTMLConfiguration.java:394)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at com.gargoylesoftware.htmlunit.html.parser.neko.HtmlUnitNekoDOMBuilder.parse(HtmlUnitNekoDOMBuilder.java:760)
at com.gargoylesoftware.htmlunit.html.parser.neko.HtmlUnitNekoHtmlParser.parseFragment(HtmlUnitNekoHtmlParser.java:158)
at com.gargoylesoftware.htmlunit.html.parser.neko.HtmlUnitNekoHtmlParser.parseFragment(HtmlUnitNekoHtmlParser.java:112)
at com.gargoylesoftware.htmlunit.javascript.host.Element.parseHtmlSnippet(Element.java:868)
at com.gargoylesoftware.htmlunit.javascript.host.Element.setInnerHTML(Element.java:920)
at com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement.setInnerHTML(HTMLElement.java:676)
at sun.reflect.GeneratedMethodAccessor17.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at net.sourceforge.htmlunit.corejs.javascript.MemberBox.invoke(MemberBox.java:188)
... 30 more


The above `... 30 more` means that I do not know the line of my code that causes the problem. How can I get the full stack trace.
Also, if it does not seem to be running any of my catch statements with `printStackTrace`, but if I turn off logging with the below command 
```java
java.util.logging.Logger.getLogger(&quot;com.gargoylesoftware.htmlunit&quot;).setLevel(java.util.logging.Level.OFF);
java.util.logging.Logger.getLogger(&quot;org.apache.http&quot;).setLevel(java.util.logging.Level.OFF);
**Have put in my full code below as suggested by the comments below . Did not put in the actual URL though**
import com.gargoylesoftware.htmlunit.html.*;
import java.io.File;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
public class download_to_send_to_stackoverflow
{
public static void main(String args[])
{
String url = &quot;did not want to include the actual web site &quot;;
HtmlPage page = null;
WebClient webClient = new WebClient();
webClient.getOptions().setRedirectEnabled(true);  
webClient.getOptions().setJavaScriptEnabled(true);  // tried making false - but didnt work
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setTimeout(30000);
webClient.setJavaScriptTimeout(30000); //e.g. 50s
webClient.waitForBackgroundJavaScript(15000) ;   // added 2017/1/24
webClient.waitForBackgroundJavaScriptStartingBefore(30000) ;   // added 2017/1/24
try
{
page = webClient.getPage( url );
savePage_just_html(page );  
}
catch( Exception e )
{
System.out.println( &quot;Exception thrown:----------------------------------------&quot; + e.getMessage() );
e.printStackTrace();
}
}
protected static String savePage_just_html(HtmlPage page)
{
try
{
File saveFolder = new File(&quot;spool/_&quot;);
page.save(saveFolder);   // this is the line causing the error with limit stack trace
}
catch ( Exception e )
{
System.out.println( &quot;IOException was thrown: ---------------------------------- &quot; + e.getMessage() );
e.printStackTrace();
}
return &quot;file.html&quot;;
}
}
------------------------------------------------------
page.save(saveFolder);   above is the problem line.  if I take it out I dont get the limited statcktrace errors but I also dont save the html page - which I want to do
- The question is - why does this line only print a limited stacktrace
</details>
# 答案1
**得分**: 2
"The &quot;Caused by&quot; means that this is a nested exception."
"The &quot;... 30 more&quot; in a nested exception stacktrace means that those 30 lines are the same as in the primary exception."
"So just look at the primary stacktrace to see those stack frames."
------------------------
"Apparently your exception stacktraces are being generated in a way that is suppressing (or removing) the primary exception stacktrace. This is puzzling, but it is probably being done by one of the unit testing libraries that you are using."
"Getting to the bottom of this will probably involve you either debugging whatever it is that is generating the stacktrace, or writing a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) so that someone else can debug it."
<details>
<summary>英文:</summary>
The &quot;Caused by&quot; means that this is a nested exception.
The &quot;... 30 more&quot; in a nested exception stacktrace means that those 30 lines are the same as in the primary exception.
So just look at the primary stacktrace to see those stack frames.
------------------------
Apparently your exception stacktraces are being generated in a way that is suppressing (or removing) the primary exception stacktrace.  This is puzzling, but it is probably being done by one of the unit testing libraries that you are using.
Getting to the bottom of this will probably involve you either debugging whatever it is that is generating the stacktrace, or writing a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) so that someone else can debug it.
</details>

huangapple
  • 本文由 发表于 2020年8月14日 12:35:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63406604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定