0

我有一个.exe文件,它将.txt文件作为输入,并将.txt文件作为输出返回。

我有 2 个文件夹名称是InputFilesExeFolder

InputFiles文件夹有很多输入文件,这些文件作为参数传递给.Exe文件。

ExeFolder.exe文件,output文件和只有一个Input文件(我们将从文件InputFiles夹中获取此文件)。

我想构建 1 个 Web 应用程序,它将按以下方式工作。

步骤1 :

它检查,就我的要求而言,sourceDirectory 中有多少文件可用。通常每次我想查找.txt文件时,但为了我的代码维护,我filetype作为参数传递给函数。

为此,我编写了以下代码,它工作正常

 public List<File> ListOfFileNames(String directoryPath,String fileType)
{
    //Creating Object for File class
    File fileObject=new File(directoryPath);
    //Fetching all the FileNames under given Path
    File[] listOfFiles=fileObject.listFiles();
    //Creating another Array for saving fileNames, which are satisfying as far our requirements
    List<File> fileNames = new ArrayList<File>();
    for (int fileIndex = 0; fileIndex < listOfFiles.length; fileIndex++) 
    {
        if (listOfFiles[fileIndex].isFile())
        {
          //True condition,Array Index value is File
          if (listOfFiles[fileIndex].getName().endsWith(fileType)) 
          {
              //System.out.println(listOfFiles[fileIndex].getName());
              fileNames .add(listOfFiles[fileIndex]);
          }
        }  
    }
    return fileNames;
}

第2步:

我使用for基于长度的循环ListOfFileNames[dir,filetype],对于每个迭代file都将被覆盖在ExeFolder文件夹中。为此,我编写了以下函数。它工作正常

  public void FileMoving(File sourceFilePath,String destinationPath,String fileName)throws IOException 
 {
File destinationPathObject=new File(destinationPath);
if (
        (destinationPathObject.isDirectory())&&
        (sourceFilePath.isFile())
    )
    //both source and destination paths are available 
    {
        //creating object for File class
        File statusFileNameObject=new File(destinationPath+"/"+fileName);
        if (statusFileNameObject.isFile())
            //Already file is exists in Destination path
            {
                //deleted File
                statusFileNameObject.delete();
                //paste file from source to Destination path with fileName as value of fileName argument
                FileUtils.copyFile(sourceFilePath, statusFileNameObject);
            }
            //File is not exists in Destination path.
            {
                //paste file from source to Destination path with fileName as value of fileName argument
                FileUtils.copyFile(sourceFilePath, statusFileNameObject);
            }
    }
}

第 3 步:

.exe文件将被运行。为此,我编写了以下函数。工作正常,但在这个函数中我需要添加一些代码等待。

   public void ExeternalFileProcessing(String DirectoryPath,String exeFilePath,String inputFileName) throws IOException
  {
//Creating Absolute file path of the executableFile
String executableFileName = DirectoryPath+"/"+exeFilePath;
//Assinging the InputFileName argument value to inputFile Variable
String inputFile=inputFileName;
//creating ProcessBuilderObject with 2 arguments
ProcessBuilder processBuilderObject=new ProcessBuilder(executableFileName,inputFile);
//creating object
File absoluteDirectory = new File(DirectoryPath);
//Assinging 
processBuilderObject.directory(absoluteDirectory);
//starting process
processBuilderObject.start();
//
//processBuilderObject.wait();
 }

第4步:

一旦.exe过程完成,则只会开始下一次迭代。这意味着我们需要监控.exe过程,无论它是否完成。

为了集成,我将以下函数名称写为Integration.

  public void Integration(String fileType,String sourcePath,String directoryPath,String executableName,String inputFileName)throws IOException
  {
    //created object for Class
    ExternalFileExecutions ExternalFileExecutionsObject=new ExternalFileExecutions();
    //calling Method from class object
    List<File> finalListNames=ExternalFileExecutionsObject.ListOfFileNames(sourcePath,fileType);
    for (int fileIndex = 0; fileIndex < finalListNames.size(); fileIndex++) 
    {
        //Copy and pasting file from SourcePath to destination Path
        ExternalFileExecutionsObject.FileMoving(
                                                    finalListNames.get(fileIndex),
                                                    directoryPath,
                                                    inputFileName
                                                );
        //Form here,.exe process will be start
        ExternalFileExecutionsObject.ExeternalFileProcessing(directoryPath,executableName,inputFileName);
    }
 }

我以以下方式调用这些,在我的Integration函数中起作用。main()

public static void main(String[] args) throws IOException
{
//created object for Class
ExternalFileExecutions ExternalFileExecutionsObject=new ExternalFileExecutions();
ExternalFileExecutionsObject.Integration(
                                            ".txt",
                                            "C:/Users/Infratab Bangalore/Desktop/copy",
                                            "C:/Users/Infratab Bangalore/Desktop/Rods",
                                            "ThMapInfratab1-2.exe",
                                            "TMapInput.txt"
                                        );
 }

如果你观察,我的代码,除了监控,一切都完成了.exe,不管它是否完成。一旦第一次迭代过程完成,那么它允许下一次迭代。在第二次迭代中再次.exe将是 process.it 就像queue

实际上我从来没有工作过Java,但是使用stackoverflow我写了上面的函数。现在我想修复.exe监控。

我什么也没找到。

谁能帮我。

我希望你们能理解我所面临的。

谢谢

4

2 回答 2

2

要运行外部进程(.exe在本例中为程序),请忘记Runtime.exec(). 而是使用ProcessBuilder;该文档指出,这是目前启动子流程的首选方式。

于 2013-07-23T20:26:02.587 回答
0

按照这个快速教程从 java 运行 .exe。应该足够了(4页)

http://www.javaworld.com/jw-12-2000/jw-1229-traps.html?page=1

例子

import java.util.*;
import java.io.*;

public class GoodWindowsExec
{
    public static void main(String args[])
    {


        try
        {            


            Runtime rt = Runtime.getRuntime();

            Process proc = rt.exec("cmd.exe /C ping"); // executing ping through commandshell of windows

            proc.getErrorStream() // errorstream

            proc.getInputStream()  // outputstream


            int exitVal = proc.waitFor(); // wait till process ends    
        } catch (Throwable t)
          {
            t.printStackTrace();
          }
    }
}
于 2013-07-23T20:18:58.687 回答