我从命令行运行 Maven
mvn exec:java -Dexec.mainClass=org.xmlcml.svg2xml.analyzer.DocumentListAnalyzer
-Dexec.args=.
当我的程序抛出一个 RuntimeException 时,maven 不会抛出这个异常,而是抛出一个 InitationTargetException
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.RuntimeException: Only one primary allowed for 3 line textL
ineGroup
at org.xmlcml.svg2xml.text.TextLineGroup.createSuscriptTextLineList(Text
LineGroup.java:181)
at org.xmlcml.svg2xml.text.TextLineGroup.createHtml(TextLineGroup.java:2
40)
运行 Eclipse 时不会发生这种情况。
为什么会发生这种情况? (它让我感到困惑,也让我的用户感到困惑)。
最佳答案
Invoking Methods告诉我们如下:-
Reflection provides a means for invoking methods on a class. Typically, this would only be necessary if it is not possible to cast an instance of the class to the desired type in non-reflective code.
Methods are invoked with
java.lang.reflect.Method.invoke()
. The first argument isthe object instance
on which this particular method is to be invoked. (If the method is static, the first argument should be null.) Subsequent arguments are themethod's parameters
.If the underlying method throws an exception, it will be wrapped by an
java.lang.reflect.InvocationTargetException
. The method's original exception may be retrieved using the exception chaining mechanism'sInvocationTargetException.getCause()
method.
因为堆栈跟踪还告诉我们,exec:java
也使用了Reflection
。然后原始异常也被包装。请引用 org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297) 的源码以供进一步引用。
希望这会有所帮助。
关于java - 为什么maven会抛出InvocableTargetException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15966662/