java - 如何从 CMD 读取所有输出以获取日志
问题描述
我不知道如何进行这项工作,但这是问题所在。我将编写一个程序来执行一系列不同的操作。我计划输出所有信息,包括调试信息和所有操作完成。如果我检测到错误,我想关闭程序,然后读取所有输出并创建一个包含所有输出的文件,以便找出问题所在。我想从命令提示符读取所有输出并将其写入带有日期和时间的日志。请不要将此标记为重复,因为我找不到与此相关的答案的任何类似问题。
解决方案
Those are some loose lines relevant to your endeavour:
public class StartProcess {
public static void main(final String[] args) throws IOException, InterruptedException {
final Process process = Runtime.getRuntime().exec("the app command");
final InputStream in = process.getInputStream();
final OutputStream out = process.getOutputStream();
final InputStream err = process.getErrorStream();
process.waitFor(); // wait for termination
final int exitCode = process.exitValue();
}
}
However, you should keep in mind to read the input from the streams asynchronously.
推荐阅读
- c++ - 如何按字母顺序将值插入数组?
- python - 使用 Bash 脚本(Ubuntu 上的 Gnome)打开终端选项卡时消失
- javascript - 按钮效果仅在功能结束后生效
- reactjs - React + Typescript:仅来自 `cloneElement` 的道具
- javascript - 在 JavaScript 中将日期时间字符串转换为 UTC 日期
- webview2 - 具有 WebView2 运行时依赖项的 C# 安装程序
- angular - 在 appflow 中构建 app 离子电容器时出错
- amazon-web-services - AWS 云中的视频编辑和存储
- kubernetes - 在ansible上创建一个隐藏目录
- ios - 使用 SwiftUI 3 .badge 修饰符时更新 TabView 徽章会重新加载所有视图