Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pipe the Java console output to a file?

I found a bug in an application that completely freezes the JVM. The produced stacktrace would provide valuable information for the developers and I would like to retrieve it from the Java console. When the JVM crashes, the console is frozen and I cannot copy the contained text anymore.

Is there way to pipe the Java console directly to a file or some other means of accessing the console output of a Java application?

Update: I forgot to mention, without changing the code. I am a manual tester.

Update 2: This is under Windows XP and it's actually a web start application. Piping the output of

javaws jnlp-url
does not work (empty file).
like image 726
Cedric Meury Avatar asked Mar 09 '09 14:03

Cedric Meury


People also ask

How do I save the console output of a file?

Select the console log text, copy and paste it to any text editor (for example, Notepad) and save it as a text file.

How do I save console output to string Java?

If you create a PrintStream connected to a ByteArrayOutputStream , then you can capture the output as a String . Example: // Create a stream to hold the output ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos); // IMPORTANT: Save the old System. out!

How do you write a command output to a file?

Redirect Output to a File Only To redirect the output of a command to a file, type the command, specify the > or the >> operator, and then provide the path to a file you want to the output redirected to. For example, the ls command lists the files and folders in the current directory.

How do I copy the console output to a text file in eclipse?

For that, go to Run → Debug Configurations on Eclipse menu. Then under Standard Input and Output section, click on checkbox next to File: , and choose the name of output file to use. If you check Append underneath, console output will be appended to the output file.


2 Answers

(If you can modify the code) you can set the System.out field to a different value:

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

If you are running a script (invoking the program via java) from Unix you could do:

/path/to/script.sh >& path/to/output.log
like image 30
oxbow_lakes Avatar answered Oct 24 '22 03:10

oxbow_lakes


Actually one can activate tracing in the Java Control Panel. This will pipe anything that ends up in the Java console in a tracing file.

The log files will end up in:

  • <user.home>/.java/deployment/log on Unix/Linux
  • <User Application Data Folder>\Sun\Java\Deployment\log on Windows
  • /~/Library/Caches/Java/log on OS X
like image 83
Cedric Meury Avatar answered Oct 24 '22 03:10

Cedric Meury