Running multiple Batch files from different threads Java

0

I have n number of thread getting created at run time according to the input source files present in a folder. For every thread, I have one common class which has all the functions present that are used by every thread. Every thing is working perfectly except the part where batch files are run.

I have main class which is creating thread(which is working perfectly fine). Then I am creating batch files with relevant contents( which is also running perfectly). After that, only 1(can be anyone, no specific pattern) thread is able to execute the batch file and not the others.

Code:

String batch_content = "echo off \n " 
                    + "powershell.exe -file "
                    + utility_path + "convertCSVSwiss.ps1 " + fpath + filename + " -executionpolicy Unrestricted \n ";
                String batch_name = "batch_" + fname +"_"+sdf.format(cal.getTime())+ ".bat";

                    Utils.createBatchFile(batch_content, bat_file_path, batch_name);
                    Utils.RunBatch(bat_file_path, batch_name,csv_file_path,fname);

Utils.createBatchFile is working fine which create a batch file with the batch content. But Utils.RunBatch seems to having some problem. Here is the code for RunBatch:

public static void RunBatch(String filepath, String filename,String csv_file_path,String fname) throws Exception {
    try {

        System.out.println("Started Program");
        new File(csv_file_path + "\\" + fname).mkdir();

        String filePath1 = filepath + filename;
        System.out.println("Batch file running is " + filePath1);
        Process p = Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", filePath1 });
        p.getOutputStream().close();
        p.waitFor();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

My log file prints this:

Batch file running is C:\ER\ETL\bat files\batch_Sample_Data_10_40_16_12_40_37.bat
Batch file running is C:\ER\ETL\bat files\batch_ssd_10_40_16_12_40_37.bat

but it runs only the first one.

Any help would be appreciated.

P.S I am sorry if I missed any information that may be necessary to get this problem resolved. Please let me know and I can then edit my post.

EDIT:

Here is my code.

    //main class to start new thread for every excel file present in the source directory

    public class LoadData{
        public static void main(String[] args) throws Exception{

    try{
        File folder = new File(fpath);
            File[] listoffiles = folder.listFiles();
            for (int i = 0; i < listoffiles.length; i++) {
                  if (listoffiles[i].isFile()) {
                      filename = listoffiles[i].getName();
                      c = filename.lastIndexOf(".");
                      absfilename = filename.substring(0, c);

                      System.out.println("File name with extension is "+filename);
                      System.out.println("File name is "+absfilename);
                      System.out.println("Starting thread for "+absfilename);

                      ConvertToCSV et = new ConvertToCSV();
                      et.fpath = fpath;
                      et.utility_path=utility_path;
                      et.filename=filename;
                      et.fname = absfilename;
                      et.bat_file_path =bat_file_path;
                      et.tpath =tpath;
                      et.csv_file_path=csv_file_path;
                      Thread t = new Thread(et);
                      t.start();
                    }
                }
             }
             catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

 //class to create the batch file content   
    public class ConvertToCSV implements Runnable{
    String fpath,utility_path,filename,fname,bat_file_path,tpath,csv_file_path;

        try{
            String batch_content = "echo off \n " 
                        + "powershell.exe -file "
                        + path_to_powershell_script_to_convert_excel_into_csv + "convertCSVSwiss.ps1 " + path_and_name_to_the_excel_file " -executionpolicy Unrestricted \n ";
            String batch_name = "batch_" + excel_file_name +"_"+sdf.format(cal.getTime())+ ".bat";

            Utils.createBatchFile(batch_content, bat_file_path, batch_name);
            Utils.RunBatch(bat_file_path, batch_name,csv_file_path,fname);
        }
        catch (Exception e) {
                e.printStackTrace();
            }
    }


    public class Utils{


    //function to create the batch file
    public static void createBatchFile(String batch_content, String path, String batch_name) throws IOException {
            String p = path + batch_name;

            File batfile = new File(p);
            FileWriter fw = new FileWriter(batfile);
            fw.write(batch_content);
            fw.close();
        }

    //function to run the batch file
    public static void RunBatch(String filepath, String filename,String csv_file_path,String fname) throws Exception {
            try {

                System.out.println("Started Program");
                new File(csv_file_path + "\\" + fname).mkdir();

                String filePath1 = filepath + filename;
                System.out.println("Batch file running is " + filePath1);
                Process p = Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", filePath1 });
                p.getOutputStream().close();
                p.waitFor();

            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }

EDIT2: I have added the run for ConvertTO CSV. My code is doing say 10 things, and 9 of them are working fine except running two batch files with different names from the same folder

public class ConvertToCSV implements Runnable{
    String fpath,utility_path,filename,fname,bat_file_path,tpath,csv_file_path,pg_db_url,pg_db,pg_db_uid,pg_db_pwd,plpgsql_path,Log_Path;

        SimpleDateFormat sdf = new SimpleDateFormat("dd_mm_yy_hh_mm_ss");
        Calendar cal = Calendar.getInstance();      

        @Override
        public void run() {

            try {
                runConvertToCSV(fpath,utility_path,filename,fname,bat_file_path,tpath,csv_file_path,plpgsql_path);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

private void runConvertToCSV(String fpath,String utility_path,String filename,String fname,String bat_file,String tpath,String csv_file_path,String plpgsql_path) throws Exception{try{
            String batch_content = "echo off \n " 
                        + "powershell.exe -file "
                        + path_to_powershell_script_to_convert_excel_into_csv + "convertCSVSwiss.ps1 " + path_and_name_to_the_excel_file " -executionpolicy Unrestricted \n ";
            String batch_name = "batch_" + excel_file_name +"_"+sdf.format(cal.getTime())+ ".bat";

            Utils.createBatchFile(batch_content, bat_file_path, batch_name);
            Utils.RunBatch(bat_file_path, batch_name,csv_file_path,fname);
        }
        catch (Exception e) {
                e.printStackTrace();
            }
}

EDIT3#: My guess was that maybe because all the batch files are trying to access the same powershell script, that is why it is not working. But then i created ps script for every batch file. Also, added error stream to the stdout to check if there is any error and this is what i am getting:

Standard Error:
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\ER\ETL\ETL_SOURCE\convertCSVSwiss_Swiss_Sample_Data.ps1:24 char:2
+     $Worksheet.SaveAs($ExtractedFileName,6)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

there are number of same error at different line. NOTE: It is the same ps script for all the batch files, it runs only for one and not for others. and that one can be anyone(no pattern). If i run the above batch file manually, then it succeeds.

java
windows
multithreading
batch-file
asked on Stack Overflow Jul 10, 2016 by Aman Rathi • edited Jul 11, 2016 by Aman Rathi

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0