读书人

java调用console程序打开文件的有关问

发布时间: 2012-10-14 14:55:08 作者: rapoo

java调用console程序打开文件的问题
我用如下的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;
}



[解决办法]
换成bat调用文件就好了

读书人网 >J2EE开发

热点推荐