Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below code i am using for it ..but log file is empty post bat script completed
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 /c start cmd.exe /K \"cd C:\\FICO\\SironKYC\\system\\web_client\\ && start C:\\FICO\\SironKYC\\system\\web_client\\kycconfig.bat>Yogesh.txt");

          //  process = runtime.exec("cmd");
        }
        catch (IOException exception) {
            exception.printStackTrace();
            return;
        }
        
        try (FileOutputStream fstdc = new FileOutputStream("C:\\FICO\\output.log");
                FileOutputStream ferrc = new FileOutputStream("C:\\FICO\\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();
        }
    }
}


What I have tried:

Please suggest to resolve the issue
Posted
Updated 18-May-20 23:09pm
Comments
Richard MacCutchan 19-May-20 3:41am    
Have you used the debugger to check that the write statements are actually being executed?

1 solution

I think you are missing some double double-quotes at then end of your command string. Moreover, I am not sure about the double cmd / start chaining, you may be overthinking the whole thing.
Try
Java
process = runtime.exec("cmd.exe /C \"cd C:\\FICO\\SironKYC\\system\\web_client\\ && kycconfig.bat > Yogesh.txt\"");
instead.
 
Share this answer
 

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