Click here to Skip to main content
15,884,032 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have server which run through CMD and generates live logs data. How do I copy these data from CMD console to a log file, placed in a local drive, using java?


Please help.
Posted
Updated 18-May-20 20:18pm
Comments
[no name] 14-Jul-14 11:14am    
Redirect the output to your log file.
Member 10885013 14-Jul-14 11:59am    
I meant how do I do it through java program.
Sergey Alexandrovich Kryukov 14-Jul-14 12:51pm    
Why?
—SA
Sergey Alexandrovich Kryukov 14-Jul-14 12:51pm    
Why? Why not redirecting your console output to file in first place?
—SA
Member 10885013 14-Jul-14 19:20pm    
Beacause, my CMD console is a server in Brisbane. I cannot modify that.

1 solution

One way would be to use a Java™ Runtime class to create an native console instance. This would allow you to retrieve all the information that would normally be output in the console.

The following example shows how to get a Runtime object, get the Process resulting from executing a command, and how to manage the streams for the process, as well as the streams for the file(s).

The example is an example for displaying the directory entries (Microsoft™ Windows®).

Java
package codeproject.console;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class SampleProgram {
    
    public static boolean isAlive(Process process) {
        try {
            process.exitValue();
            return false;
        }
        catch (IllegalThreadStateException e) {
            return true;
        }
    }
    
    public static void main(String[] args) {
        Runtime runtime = Runtime.getRuntime();
        Process process;
        int value;
        
        try {
            process = runtime.exec("cmd");
        }
        catch (IOException exception) {
            exception.printStackTrace();
            return;
        }
        
        try (FileOutputStream fstdc = new FileOutputStream("output.log");
                FileOutputStream ferrc = new FileOutputStream("error.log");
                OutputStream outc = process.getOutputStream();
                InputStream inc = process.getInputStream();
                InputStream errc = process.getErrorStream()) {
            outc.write("dir\r\n".getBytes());
            outc.flush();
            outc.write("exit\r\n".getBytes());
            outc.flush();
            
            while (isAlive(process) == true) {
                
                if (inc.available() == 0 && errc.available() == 0) {
                    try {
                        Thread.sleep(100);
                    }
                    catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                
                if (inc.available() > 0) {
                    while (inc.available() > 0) {
                        value = inc.read();
                        System.out.print((char) value);
                        fstdc.write((char) value);
                    }
                }
                
                if (errc.available() > 0) {
                    while (errc.available() > 0) {
                        value = errc.read();
                        System.err.print((char) value);
                        ferrc.write((char) value);
                    }
                }
            }
        }
        catch (IOException exception) {
            exception.printStackTrace();
        }
    }
}


For simple time-stamp logging, with basic user input support:
Java
package codeproject.console;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;

public class SampleTimeLogProgram {
    
    /** Returns the date-time stamp */
    public static String timeStamp() {
        return String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM", Calendar.getInstance());
    }
    
    /** Returns true if process is still alive, false otherwise. */
    public static boolean isAlive(Process process) {
        try {
            process.exitValue();
            return false;
        }
        catch (IllegalThreadStateException e) {
            return true;
        }
    }
    
    public static void main(String[] args) {
        Runtime runtime = Runtime.getRuntime();
        Process process;
        InputStream userInput = System.in;
        int value;
        
        try {
            process = runtime.exec("cmd");
        }
        catch (IOException exception) {
            exception.printStackTrace();
            return;
        }
        
        try (FileOutputStream fstdc = new FileOutputStream("output.log", true);
                FileOutputStream ferrc = new FileOutputStream("error.log", true);
                OutputStream outc = process.getOutputStream();
                InputStream inc = process.getInputStream();
                InputStream errc = process.getErrorStream()) {
            
            String stamp = timeStamp();
            fstdc.write(("\r\n" + stamp + " ").getBytes());
            ferrc.write(("\r\n" + stamp + " ").getBytes());
            System.out.print(stamp + " ");
            
            while (isAlive(process) == true) {
                
                if (inc.available() == 0 && errc.available() == 0) {
                    if (userInput != null && (userInput.available() > 0)) {
                        value = userInput.read();
                        outc.write((char) value);
                        outc.flush();
                    }
                    else {
                        try {
                            Thread.sleep(100);
                        }
                        catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                
                if (inc.available() > 0) {
                    while (inc.available() > 0) {
                        value = inc.read();
                        fstdc.write((char) value);
                        System.out.print((char) value);
                        
                        if (value == '\n') {
                            stamp = timeStamp();
                            fstdc.write((stamp + " ").getBytes());
                            System.out.print(stamp + " ");
                        }
                        
                        System.out.flush();
                    }
                }
                
                if (errc.available() > 0) {
                    while (errc.available() > 0) {
                        value = errc.read();
                        ferrc.write((char) value);
                        System.err.print((char) value);
                        
                        if (value == '\n') {
                            stamp = timeStamp();
                            ferrc.write((stamp + " ").getBytes());
                            System.err.print(stamp + " ");
                        }
                        
                        System.err.flush();
                    }
                }
            }
        }
        catch (IOException exception) {
            exception.printStackTrace();
        }
    }
}
 
Share this answer
 
v3
Comments
Member 10885013 16-Jul-14 19:29pm    
This is great Osmund Sir. And one more thing, if I have to filter the last one hour data from the log files generated from the CMD console then what should I do? I have written a program which will read the log files backward, but I am not getting a way to parse the last one hour data.
Osmund Francis 17-Jul-14 10:09am    
Logging with the ability to filter is not a trivial task.

I've updated my solution with a code snippet that simply adds a date-time stamp every time a new-line character is detected.
Member 10885013 16-Jul-14 23:03pm    
The code

while (process.isAlive() == true) {

is giving a compilation error as "The method isAlive() is undefined for the type Process"
Osmund Francis 17-Jul-14 9:45am    
I apologise, the isAlive() method is a Java™ 8 feature. I'll update my solution.
Member 10885013 18-Jul-14 4:11am    
Thank you for your valuable suggestion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900