如何在没有 Java Web Start 的情况下将 Java 控制台输出通过管道传输到文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18669245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:01:21  来源:igfitidea点击:

How Can I Pipe the Java Console Output to File Without Java Web Start?

java

提问by Thunderforge

I am wanting to pipe the Java console output (generated by System.out.printlnand its ilk) to a file. I found an excellent solution hereto enable Java tracing, but this isn't working for me (no log file shows up in any location on Mac OS X or Windows). From what I can tell, this is because I'm using a plain Java app without Java web start. So how can I do this with Java code that does not use Java web start? Ideally, I would like a solution that does not require modifying code.

我想将 Java 控制台输出(由System.out.println及其同类生成)通过管道传输到文件。我在这里找到了一个很好的解决方案来启用 Java 跟踪,但这对我不起作用(Mac OS X 或 Windows 上的任何位置都没有显示日志文件)。据我所知,这是因为我使用的是没有 Java web start 的普通 Java 应用程序。那么我如何使用不使用 Java web start 的 Java 代码来做到这一点呢?理想情况下,我想要一个不需要修改代码的解决方案。

采纳答案by Sotirios Delimanolis

You don't require Java web start to do any of this. Just set the System.outto a FileOutputStream.

您不需要 Java web start 来执行任何这些操作。只需将 设置System.out为 a FileOutputStream

System.setOut(new PrintStream(new FileOutputStream(fileName)));

where fileNameis the full path to the file you want to pipe to.

fileName您要通过管道传输到的文件的完整路径在哪里。

public static void main(String[] args) throws Exception {   //Read user input into the array
    System.setOut(new PrintStream(new FileOutputStream("home.txt")));
    System.out.println("hello");
}

Will write hello\nto a file named home.txtin the current working directory.

将写入在当前工作目录中hello\n命名的文件home.txt

If you can't modify code, on Windows 7, use command redirection.

如果您无法修改代码,请在 Windows 7 上使用command redirection

java YourMainClass > home.txt

If you need to run a jar, you can use the -jaroption of the java application launcher.

如果需要运行 jar,可以使用java application launcher-jar选项。

java -jar /your/path.jar > /output/file/path.txt

回答by Stephen C

Is there any way that I can do it without modifying code? I'd like to do it for an application that is already compiled.

有没有什么方法可以在不修改代码的情况下做到这一点?我想为已经编译的应用程序做这件事。

If you want to do it without changing code, then your best option is this:

如果您想在不更改代码的情况下执行此操作,那么您最好的选择是:

$ java com.example.MyMainClass > filename

回答by Katona

If you launch it from command line, then you can use redirect stdout and stderr into file as follows:

如果您从命令行启动它,那么您可以使用重定向 stdout 和 stderr 到文件中,如下所示:

java -jar application.jar >file.txt  2>&1

where you have to replace application.jarwith the jar file of your application.

您必须将application.jar替换为应用程序的 jar 文件。

回答by Zhefu Zhangzhu

If you want to run the problem in the background, do this -

如果您想在后台运行问题,请执行以下操作 -

nohup java -jar yourprogram.jar > log.txt 2>&1 &