• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to execute batch file & .exe file using jsp servlet?  RSS feed

 
vaibhav kandale
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question : How to execute batch file & .exe file using jsp servlet?
I am giving input for blast in the FASTA format. After that i wants to make the sequence.FASTA file in my project folder. And I wants to execute batch file with parameters as blastp.exe file, sequence.FASTA file, database files, output file.

There is no problem in the file creation of sequence.FASTA

ERROR IN: the batch file is not execute, So the I am not able to get the output file for display.

ERROR is : java.io.FileNotFoundException: D:\vaibhav_workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp4\wtpwebapps\toxin-data\outputprotein.txt (The system cannot find the file specified)


Technologies Used :JSP,Servlet,Process-builder

I have the following code as :

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String values=request.getParameter("t1");

ServletContext servletContext = request.getSession().getServletContext();
servletContext = request.getSession().getServletContext();
String path="//sequence.FASTA"; /*file is created in getRealPath()*/
String uploadFile=servletContext.getRealPath(path);

File outputFile = new File(uploadFile);
FileWriter fout = new FileWriter(outputFile);
fout.write(values);
fout.close();

servletContext = request.getSession().getServletContext();
String blastPath="//blast.bat";
String blastBat_path=servletContext.getRealPath(blastPath);
Process process = new ProcessBuilder(blastBat_path).start();
try {
process.waitFor();
} catch (InterruptedException e) {e.printStackTrace();}
process.destroy();

/* to read output file for display */
BufferedReader br1=null;
br1= new BufferedReader(new FileReader(servletContext.getRealPath("//outputprotein.txt")));
try {
StringBuilder sb = new StringBuilder();
String line1 = br1.readLine();

while (line1 != null)
{
sb.append(line1);
sb.append(System.lineSeparator());
line1 = br1.readLine();
}
String everything = sb.toString();
out.println("<pre>"+everything+"</pre>");
} finally {
br1.close();
}
fout.close();
}


Here the getRealpath() is:

D:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp4\wtpwebapps\toxin-data

My all files in the project folder as toxin-data
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are trying to do too much at one pass. Guaranteed to cause frustration.

First you need to get comfortable with executing a batch file and handling all the things that can go wrong there. NOT in the servlet / jsp environment but as a utility class. With some thought you can use this utility to test with a variety of input parameters.

Read this classic article on using exec()

You can also find plenty of examples here in the forums on using exec() and process.

 
Madhav Turangi
Greenhorn
Posts: 16
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Ranch Vaibhav.

I see the path in error message

ERROR is : java.io.FileNotFoundException: D:\vaibhav_workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp4\wtpwebapps\toxin-data\outputprotein.txt (The system cannot find the file specified)


is different to what you said the path should be.


Here the getRealpath() is:

D:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp4\wtpwebapps\public_html

My all files in the project folder as public_html


As William suggested, decouple the Process handling code out of your servlet code.

I would parametrize the path and ensure files are generated into given path as expected by process handling code.
Then get a better understanding about paths related to your web-app, and workout correct paths to use in servlet code, so the above mismatch does not occur.

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!