问题
I have a .exe
file, It takes .txt
file as a Input and it returns .txt
files as outputs.
I have 2 folders names are InputFiles
and ExeFolder
.
InputFiles
folder have so many number of input files, which files are passing as a argument to .Exe
file.
ExeFolder
have .exe
file,output
files and only one Input
file(we will get this file from InputFiles
folder).
I want to build 1 web Application, It will work in the following way.
step 1 :
it checks, How many no of files are available in sourceDirectory as far my requirements.generally every time I want to find .txt
files but for my code maintenance I passed filetype
as a argument to function.
For this,I wrote the following code,it's working fine
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;
}
step 2:
I used for
loop Based on length of ListOfFileNames[dir,filetype]
,For every Iteration file
will be overwrite in ExeFolder
folder. For this I wrote the following function.It's working fine
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);
}
}
}
Step 3 :
.exe
file will be run.For this I wrote the following function.Working fine but in this function I need to add some code for waiting.
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();
}
Step 4:
once .exe
process was done, then only next Iteration will be start. That means we need to monitor .exe
process,whether is it done or not.
for integration, I wrote the following function name as 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);
}
}
I called these, Integration
function in my main()
in the following way.
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"
);
}
If you observer, my code,every thing was done except .exe
monitoring,whether is it completed or not.Once first iteration process was done then it allows next Iteration.In second Iteration again .exe
will be process.it just like queue
.
Actually I never work on Java
,but using stackoverflow
I wrote the above functions. Now I want to fix .exe
monitoring.
I didn't found anything.
can anyone help me.
I hope, you guys understand what I am facing.
Thanks
回答1:
For running an external process (a .exe
program in this case), forget about Runtime.exec()
. Instead use ProcessBuilder; the documentation states that it's the preferred way to start up a sub-process these days.
回答2:
Follow this quick tutorial over running .exe from java. It should be sufficient (4pages)
http://www.javaworld.com/jw-12-2000/jw-1229-traps.html?page=1
example
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();
}
}
}
来源:https://stackoverflow.com/questions/17804790/how-can-i-monitor-external-files-in-java