从不同的线程Java运行多个批处理文件

时间:2016-07-10 07:14:20

标签: java windows multithreading batch-file

根据文件夹中的输入源文件,我在运行时创建了n个线程。对于每个线程,我有一个公共类,它具有每个线程使用的所有函数。除了运行批处理文件的部分外,每件事情都完美无缺。

我有创建线程的主类(工作正常)。然后我正在创建具有相关内容的批处理文件(它也运行得很好)。之后,只有1个(可以是任何人,没有特定模式)线程能够执行批处理文件而不能执行其他文件。

代码:

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工作正常,可以创建包含批处理内容的批处理文件。但是Utils.RunBatch似乎有一些问题。以下是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();
    }

}

我的日志文件打印出来:

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

但它只运行第一个。

任何帮助都将不胜感激。

如果我错过了解决此问题可能需要的任何信息,我很抱歉。请让我知道,然后我可以编辑我的帖子。

编辑:

这是我的代码。

    //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:我已添加了ConvertTO CSV的运行。我的代码正在做10件事,其中9件工作正常,除了在同一个文件夹中运行两个具有不同名称的批处理文件

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#: 我的猜测可能是因为所有批处理文件都试图访问相同的PowerShell脚本,这就是它无法正常工作的原因。但后来我为每个批处理文件创建了ps脚本。此外,向stdout添加了错误流以检查是否有任何错误,这就是我得到的:

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

在不同的行上有相同的错误数。 注意:它与所有批处理文件的ps脚本相同,只运行一个而不运行其他文件。那个人可以是任何人(没有模式)。 如果我手动运行上面的批处理文件,那么它会成功。

0 个答案:

没有答案