Parses a java stack trace to access package, class, method, file, and line information of every stack trace element.
- Add the JitPack repository to your
pom.xml
file:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
- Add the dependency:
<dependency>
<groupId>com.github.alexscheitlin</groupId>
<artifactId>java-stack-trace-parser</artifactId>
<version>v1.0.1</version>
</dependency>
- Add the JitPack repository to your
build.gradle
file:
repositories {
...
maven { url 'https://jitpack.io' }
}
- Add the dependency:
dependencies {
implementation 'com.github.alexscheitlin:java-stack-trace-parser:v1.0.1'
}
The StackTraceParser
class parses a java stack trace (e.g. below) and maps it to a StackTrace
object with java.lang.StackTraceElement
s.
The example below can be found here and the dummy stack trace here.
java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:86)
at org.junit.Assert.assertTrue(Assert.java:41)
at org.junit.Assert.assertTrue(Assert.java:52)
at com.example.bank.BankAccountTest.getNumber(BankAccountTest.java:21)
...
First read some dummy stack trace:
String stackTraceString = null;
try {
stackTraceString = ResourceReader.readResourceFile("dummyStackTrace.txt");
} catch (FileNotFoundException e) {
System.out.println("Could not read test resource file!");
return;
}
Then parse the string to map it to a StackTrace
object.
StackTrace stackTrace = null;
try {
stackTrace = StackTraceParser.parse(stackTraceString);
} catch (Exception e) {
System.out.println("Could not read test resource file!");
}
To access the first stack trace line (the line indicating the caused error) use the following command:
String firstLine = stackTrace.getFirstLine();
To get detailed information about some stack trace line use the following command:
StackTraceElement stackLine = stackTrace.getStackTraceLines().get(0);
System.out.println("First line:\t" + firstLine);
System.out.println("Stack Line:\t" + stackLine.toString());
System.out.println("\tDeclaring class:\t" + stackLine.getClassName());
System.out.println("\tMethod name:\t\t" + stackLine.getMethodName());
System.out.println("\tFile name:\t\t\t" + stackLine.getFileName());
System.out.println("\tLine number:\t\t" + stackLine.getLineNumber());
System.out.println("\tIs Native Method:\t" + stackLine.isNativeMethod());
Expected output:
Stack Line: org.junit.Assert.fail(Assert.java:86)
Declaring class: org.junit.Assert
Method name: fail
File name: Assert.java
Line number: 86
Is Native Method: false
Original stack trace line: at org.junit.Assert.fail(Assert.java:86)
To get stack trace lines of a specific package use the following command:
String packageName = "com.example.bank";
List<StackTraceElement> linesOfPackage = stackTrace.getLinesOfPackage(packageName);
for (StackTraceElement line : linesOfPackage) {
System.out.println(line.toString());
}
Expected output:
com.example.bank.BankAccountTest.getNumber(BankAccountTest.java:21)
To reconstruct the original stack trace use the following command:
System.out.println(stackTrace.getOriginalStackTrace());
- Alex Scheitlin - Initial work - alexscheitlin
This project is licensed under the MIT License.