我用如下的servlet来调用一个exe程序,exe程序见下。 
当用tomcat运行这个servlet时发现任务管理器里闪了一下这个exe,但是exe没有打开文件,请问这是怎么回事?
package myHelloWorld;
import java.io.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;  
import java.util.Scanner; 
public class testopenfile extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws IOException,ServletException
    {
          PrintWriter out=response.getWriter();
          out.write("<html>\r\n");
          out.write("<head>\r\n");
          out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
          out.write("</head>");
                out.write("<body>\r\n");
       out.print("going to capture data on sunxudong's pc");
         String string_port_num; 
         
         String exetestpath="g:/sunAppPacket/openfile.exe";         
         String fileName="g:/sunAppPacket/netport_config.txt";
//
         BufferedReader in=new BufferedReader(new FileReader(fileName));
         string_port_num=in.readLine();
         in.close();
          run_exetest(string_port_num,exetestpath);
 
          out.write("</body>\r\n");
          out.write("</html>");
    }//fun: doget
public static void run_exetest(String string_port_num,String exepath)
{    String line;
    OutputStream stdin = null;
    InputStream stderr = null;
    InputStream stdout = null;      // launch EXE and grab stdin/stdout and stderr
       Runtime rn = Runtime.getRuntime();  
       Process p = null;  
       try {  
           
      p = rn.exec(exepath);  
      stdin = p.getOutputStream ();
      stderr = p.getErrorStream ();
      stdout = p.getInputStream ();
           
         // "write" the parms into stdin
      line = string_port_num + "\n";
      stdin.write(line.getBytes() );
      stdin.flush();
      stdin.close();
           
           
       }catch (Exception e) {  
         
       }//catch}//void run
}//class
exe程序如下,要求用户输入一段内容写到hello。txt里面#include <fstream>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
int main(){
 ofstream outfile; 
 char cCharContent[100];
 printf("please input the content to write in\n");
 scanf("%s",cCharContent); 
 outfile.open("hello.txt");
 outfile<<cCharContent;
 outfile.close();return 1;
}

解决方案 »

  1.   

    问题补充:有可能是java杀死了这个c写的exe,但是如果想办法让java停下来,则任务管理器里的exe会一直存在,但是仍旧没有创建该创建的文件
      

  2.   

    把上面的代码加上标号,方便各位高手指教,调用exe的代码在90到100行
     21 public class testopenfile extends HttpServlet
     22 {
     23     public void doGet(HttpServletRequest request, HttpServletResponse respo    nse)
     24             throws IOException,ServletException
     25     {
     26           PrintWriter out=response.getWriter();
     27           out.write("<html>\r\n");
     28           out.write("<head>\r\n");
     29           out.write("<meta http-equiv=\"Content-Type\" content=\"text/html;     charset=UTF-8\">\r\n");
     30           out.write("</head>");
     31
     32
     33       out.write("<body>\r\n");
     34
     35
     36        out.print("going to capture data on sunxudong's pc");
     37
     38
     39          String string_port_num;
     40
     40
     41          String exetestpath="g:/sunAppPacket/openfile.exe";
     42
     43
     44          String fileName="g:/sunAppPacket/netport_config.txt";
     45 //
     46          BufferedReader in=new BufferedReader(new FileReader(fileName));
     47          string_port_num=in.readLine();
     48          in.close();
     49
     50
     51
     52
     53           run_exetest(string_port_num,exetestpath);
     54
     55           out.write("</body>\r\n");
     56           out.write("</html>");
     57
     58 try {
     59 Thread.sleep(2000);
     60 } catch(InterruptedException e) {
     61 }
     62
                                                                 40,1          44%
     63
     64
     65
     66
     67
     68     }//fun: doget
     69
     70
     71
     72
     73 public static void run_exetest(String string_port_num,String exepath)
     74 {
     75
     76     String line;
     77     OutputStream stdin = null;
     78     InputStream stderr = null;
     79     InputStream stdout = null;
     80
     81       // launch EXE and grab stdin/stdout and stderr
     82
     83
     84
     85 85
     86        Runtime rn = Runtime.getRuntime();
     87        Process p = null;
     88        try {
     89
     90       p = rn.exec(exepath);
     91       stdin = p.getOutputStream ();
     92       stderr = p.getErrorStream ();
     93       stdout = p.getInputStream ();
     94
     95          // "write" the parms into stdin
     96       line = string_port_num + "\n";
     97       stdin.write(line.getBytes() );
     98       stdin.flush();
     99
    100
    101       stdin.close();
    102
    103
    104        }catch (Exception e) {
    105
    106        }//catch
    107
       }//void run
    109
    110
    111 }//class