各位兄弟
谢谢大家的帮忙
我知道这个方法并且写了两个
cmd调用的例子
一个是开网页 一个是调用 et。exe
下面是这两个程序
这个是开网页
public class TestCmd {
public TestCmd(){}
public static void main(String args[]){
try {
Process process = Runtime.getRuntime().exec("cmd.exe /c start http://www.hao123.net/"); //????
// Process process = Runtime.getRuntime().exec("cmd.exe /c start ping 10.5.2.19"); //??Ping??
}catch (Exception e)
{
e.printStackTrace();
}
}
} 这个是开et。exe的程序
import java.io.*;
import java.util.ArrayList;public class TestJavaexec
{
public static void main(String args[])
{
try
{
ArrayList list = new ArrayList(); String command = "c:\\odbc_data\\et.exe"; Process child = Runtime.getRuntime().exec(command);
OutputStream out = child.getOutputStream();
InputStream in = child.getInputStream(); BufferedReader bufRead = new BufferedReader(new InputStreamReader(in)); String data = null;
while ((data = bufRead.readLine()) != null)
list.add(data);
child.waitFor(); if (child.exitValue() != 0)
System.err.println("error!");
bufRead.close();
String[] outdata = (String[]) list.toArray(new String[0]); for (int i = 0; i < outdata.length; i++)
{
System.out.println(outdata[i]);
} } catch (IOException e)
{
e.printStackTrace();
System.out.println("exe");
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}当我在cmd 中分别输入java TestCmd和java TestJavaexec
都可以运行 这是个好现象 但问题是我要在servlet里面调用这种方法
我就是简单的 把try后面的句子剪接进去
然后开 tomcat 运行根本没反应
不知道为什么本地可以而在web上就不行了呢?
各位高手不放发表下高见
谢谢大家的帮忙
我知道这个方法并且写了两个
cmd调用的例子
一个是开网页 一个是调用 et。exe
下面是这两个程序
这个是开网页
public class TestCmd {
public TestCmd(){}
public static void main(String args[]){
try {
Process process = Runtime.getRuntime().exec("cmd.exe /c start http://www.hao123.net/"); //????
// Process process = Runtime.getRuntime().exec("cmd.exe /c start ping 10.5.2.19"); //??Ping??
}catch (Exception e)
{
e.printStackTrace();
}
}
} 这个是开et。exe的程序
import java.io.*;
import java.util.ArrayList;public class TestJavaexec
{
public static void main(String args[])
{
try
{
ArrayList list = new ArrayList(); String command = "c:\\odbc_data\\et.exe"; Process child = Runtime.getRuntime().exec(command);
OutputStream out = child.getOutputStream();
InputStream in = child.getInputStream(); BufferedReader bufRead = new BufferedReader(new InputStreamReader(in)); String data = null;
while ((data = bufRead.readLine()) != null)
list.add(data);
child.waitFor(); if (child.exitValue() != 0)
System.err.println("error!");
bufRead.close();
String[] outdata = (String[]) list.toArray(new String[0]); for (int i = 0; i < outdata.length; i++)
{
System.out.println(outdata[i]);
} } catch (IOException e)
{
e.printStackTrace();
System.out.println("exe");
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}当我在cmd 中分别输入java TestCmd和java TestJavaexec
都可以运行 这是个好现象 但问题是我要在servlet里面调用这种方法
我就是简单的 把try后面的句子剪接进去
然后开 tomcat 运行根本没反应
不知道为什么本地可以而在web上就不行了呢?
各位高手不放发表下高见
import java.net.*;
import java.util.*;
import java.text.DateFormat;
import java.util.Date;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;public class edit_exe extends HttpServlet {
// private File dir; Connection con; int exitVal = 0; public void init(ServletConfig config) throws ServletException {
super.init(config); // ////////////////////////////////
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
System.err.println("(servlet init class not found) " + e);
} try {
con = DriverManager.getConnection("jdbc:odbc:localparameter");
} catch (SQLException e) {
System.err.println("(serlvet init load fails) " + e);
} } // ///////////////////////////////////////////////////
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException { ///CmdInvacation.invoke("cmd");/////
HttpSession session = req.getSession(true); PrintWriter output = res.getWriter();
res.setContentType("text/html");
// ///// calling javac.exe /////////
try {
Process process = Runtime.getRuntime().exec("cmd.exe /c start http://www.hao123.net/"); //????
// Process process = Runtime.getRuntime().exec("cmd.exe /c start ping 10.5.2.19"); //??Ping??
}catch (Exception e)
{
e.printStackTrace();
}
// //// column ///
try { Statement stmt = con.createStatement(); String actualname = (String) session.getAttribute("actualname"); output.println("<table width=100% height=580>");
// //// left column ///
output
.println("<tr><td bgcolor=#000080 align=center valign=top width=70>");
// //////// contain ///
output.println("<table border=0 bgcolor=#191940>");
output
.println("<tr align=center ><td><form method=\"post\" action=\"edit_data1\"><INPUT TYPE=\"submit\" class=button style=font-size:7pt style=width:155px value=\"Data1\"></td></tr></form>");
output
.println("<tr align=center ><td><form method=\"post\" action=\"edit_data2\"><INPUT TYPE=\"submit\" class=button style=font-size:7pt style=width:155px value=\"Data2\"></td></tr></form>");
output
.println("<tr align=center ><td><form method=\"post\" action=\"edit_data3\"><INPUT TYPE=\"submit\" class=button style=font-size:7pt style=width:155px value=\"Data3\"></td></tr></form>");
output
.println("<tr align=center ><td><form method=\"post\" action=\"edit_data4\"><INPUT TYPE=\"submit\" class=button style=font-size:7pt style=width:155px value=\"Data4\"></td></tr></form>");
output
.println("<tr align=center ><td><form method=\"post\" action=\"edit_exe\"><INPUT TYPE=\"submit\" class=button style=font-size:7pt style=width:155px value=\"run exe\"></td></tr></form>");
output
.println("<tr align=center ><td><form method=\"post\" action=\"show_data5\"><INPUT TYPE=\"submit\" class=button style=font-size:7pt style=width:155px value=\"Data5\"></td></tr></form>");
output
.println("<tr align=center ><td><form method=\"post\" action=\"show_data6\"><INPUT TYPE=\"submit\" class=button style=font-size:7pt style=width:155px value=\"Data6\"></td></tr></form>");
output
.println("<tr align=center ><td><form method=\"post\" action=\"show_data7\"><INPUT TYPE=\"submit\" class=button style=font-size:7pt style=width:155px value=\"Data7\"></td></tr></form>");
output
.println("<tr align=center ><td><form method=\"post\" action=\"show_data8\"><INPUT TYPE=\"submit\" class=button style=font-size:7pt style=width:155px value=\"Data8\"></td></tr></form>");
output
.println("<tr align=center ><td><form method=\"post\" action=\"show_plot\"><INPUT TYPE=\"submit\" class=button style=font-size:7pt style=width:155px value=\"plot\"></td></tr></table></form>");
output.println("</td>"); // /////// right column /////
output.println("<td valign=top>");
// // right contain //////
// / title
output
.println("<table><tr><td align=center><b> <font face=\"arial\" color=\"#ff9933\" size=4>now the fortran exe programme is running , you may wait for a few seconds ,and then click the output button to see the results .If you want to see the fig ,you may press the button plot </font></td></tr></table>"); output.println("</td></tr></table>"); // member
// /////////////////////////
output.println("</Body>");
output.println("</HTML>");
} catch (SQLException e) {
log("SQL Error: " + e);
output
.println("<P>Error encountered during Database Access ...<P> Please try later ...");
} } // service public void destroy() {
try {
con.close();
} catch (SQLException e) {
System.err.println("(servlet destroy) " + e);
}
}} // class
我不太懂你的意思
所以在上面贴出了那个servlet
你帮我看看该怎么改
万分感谢
处理过程在服务器上..和浏览器的安全级别没有关系的..除非你在客户端使用了applet
问题是我用applet却实现不了
是jdk版本低 吗?
他叫我使用一个什么eclipe的东西 却能run as applet
问题是不在那里面就不行
顺便问下 eclipe有什么用 ?
所以调用命令也是在本地的
而不是在服务器上调用..
用servlet调用不就好了
我现在用applet都搞不定
用appletviewer testexec。html可以看见
但是双击 html 却看不见 写着 html load java applet failed
不知道什么原因 请给为高手指教