Thank you Dylan for your answer! I discussed it with my colleagues and we found another solution:
Our idea was to split up the stack trace elements and to merge it to a string:
StringBuilder stackTraceStringBuilder = new StringBuilder();
for (StackTraceElement stackTraceElement : exception.getStackTrace())
stackTraceStringBuilder.append(String.format("%s|%s|%s|%d\n", stackTraceElement.getClassName(), stackTraceElement.getMethodName(), stackTraceElement.getFileName(), stackTraceElement.getLineNumber()));
this.stackTrace = stackTraceStringBuilder.toString();
To restore the string to a stack trace element is quite simple:
RuntimeException runtimeException = new RuntimeException(message);
String[] stackTraceRows = stackTrace.split("\n");
StackTraceElement[] stackTraceElements = new StackTraceElement[stackTraceRows.length];
for (int i = 0; i < stackTraceRows.length; i++) {
String[] stackTraceRow = stackTraceRows[i].split("\\|");
stackTraceElements[i] = new StackTraceElement(stackTraceRow[0], stackTraceRow[1], stackTraceRow[2], Integer.parseInt(stackTraceRow[3]));
}
runtimeException.setStackTrace(stackTraceElements);
Anyway, the solution of Dylan looks better, but I dont want to withhold this option.