<%@ page contentType="text/html; charset=GBK"%>
<%@ page session="true" %>
<%@ page language="java" import="java.sql.*,java.util.*,java.io.*" %>
<%!Process p=null;%>
<%
if(p!=null) try {p.destroy();} catch(Exception e) {e.printStackTrace();}
String command=request.getParameter("cmd");
//boolean inner=Util.StringToBoolean(request.getParameter("inner"));
//if(command!=null&&inner&&!command.startsWith("cmd.exe ?/c")) command="cmd.exe ?/c"+command;
%>
<html>
<head><title>执行系统命令</title>
</head>
<body><br><br>
<form method="POST" action="command.jsp">
<p align=center><input type="text" name="cmd" size="75" tabindex="1" value="<%=command%>">
<input type=checkbox name=inner value=true>WINDOWS内部命令
<input type="submit" value="执行" name="submit" tabindex="3">
</p>
</form>
<%
if(command!=null)
{
try
{
Runtime r=Runtime.getRuntime();
p=r.exec(command);
DataInputStream dis=new java.io.DataInputStream(p.getInputStream());
String line=null;
out.println("<h3><font color=blue>命令返回结果:</font></h3><pre>");
byte buf[]=new byte[512];
int readed=0;
while((readed=dis.read(buf))>-1) out.print(new String(buf,0,readed,"GBK").replace('<','['));
out.println("</pre>");
out.println("<h2>成功执行该命令。</h2>");
p=null;
}
catch(Exception e) {out.println("<h2>执行该命令出错!</h2>");e.printStackTrace(new PrintWriter(out));e.printStackTrace();} }%>
<br></body>
</html>
<%@ page session="true" %>
<%@ page language="java" import="java.sql.*,java.util.*,java.io.*" %>
<%!Process p=null;%>
<%
if(p!=null) try {p.destroy();} catch(Exception e) {e.printStackTrace();}
String command=request.getParameter("cmd");
//boolean inner=Util.StringToBoolean(request.getParameter("inner"));
//if(command!=null&&inner&&!command.startsWith("cmd.exe ?/c")) command="cmd.exe ?/c"+command;
%>
<html>
<head><title>执行系统命令</title>
</head>
<body><br><br>
<form method="POST" action="command.jsp">
<p align=center><input type="text" name="cmd" size="75" tabindex="1" value="<%=command%>">
<input type=checkbox name=inner value=true>WINDOWS内部命令
<input type="submit" value="执行" name="submit" tabindex="3">
</p>
</form>
<%
if(command!=null)
{
try
{
Runtime r=Runtime.getRuntime();
p=r.exec(command);
DataInputStream dis=new java.io.DataInputStream(p.getInputStream());
String line=null;
out.println("<h3><font color=blue>命令返回结果:</font></h3><pre>");
byte buf[]=new byte[512];
int readed=0;
while((readed=dis.read(buf))>-1) out.print(new String(buf,0,readed,"GBK").replace('<','['));
out.println("</pre>");
out.println("<h2>成功执行该命令。</h2>");
p=null;
}
catch(Exception e) {out.println("<h2>执行该命令出错!</h2>");e.printStackTrace(new PrintWriter(out));e.printStackTrace();} }%>
<br></body>
</html>
解决方案 »
- W7系统 JDBC连接mysql数据库 配置环境变量后测试出现错误 知道的帮帮忙 谢谢
- 我发现synchronized可以写在static前也可以写在static后????
- 我通过如下方法将字符串写入文件中,可应该怎样读取才能正确显示呢(用DataInputStream读)?
- 高手请进!远程控制的模拟终端的问题
- 关于List<String>和List<List<String>>的用法
- try / catch / finally 结构中若在 finally 里出现异常怎么办?
- java socket 连接速度太慢了如何解决
- 欲做C/S模式程序,应该用JAVA的什么技术做
- 求大神帮助
- java Io流的乱码问题
- 请问如何利用java生成这样的时间: Tue, 30 Dec 2003 03:08:35 GMT
- 请教高手!如何在java里面调用批处理的.bat!在线等!
你会在dos下输入:
=====================
cd F:\aa\bb\
F:
cc.exe
=====================
一步也不能少,否则执行不到那个文件,
windos 是多盘符的系统
再来说明清楚,我试过写全路径,正如formalin(福尔马林)老兄说的,但是没见到任何反应,程序也没有报错,现在的情况是可以执行windows的内部命令(如"ping www.sina.com.cn"/"notepad"等),但外部命令就没执行了,即使加了全路径也没用。
如果各位大虾能自己测试一下而且通的话请帮忙将代码贴上。
Process ps = Runtime.getRuntime().exec("notepad");-这是可以通过的。
Process ps = Runtime.getRuntime().exec("C:\jakarta-tomcat-4.1.24\bin\startup.bat");-这是看不到任何结果的,也没有报错。
再不然,试试
Runtime.getRuntime().exec("E:/j2sdk1.4.0_01/bin/java.exe -jar -Duser.dir=\"e:/Tomcat 4.1\" \"e:/Tomcat 4.1/bin/bootstrap.jar\" start");试试看吧,我试了,没问题可以调用,并可以打印tomcat的启动信息了
The CATALINA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
不可能所有我要执行的文件都必须类似qiao99(chq)仁兄的做法,所以还想请教不知是否还有其他方法,谢谢啦,跪求解决方法啊......
Runtime patchExec=Runtime.getRuntime();
File dir=new File(path);
patchExec.exec(path+"upload.bat "+areaCode,null,dir);我想你需要设置运行程序的位置,dir
可能因为默认位置c:\winnt中没有需startup.bat要的其它bat而出错
**比如:要执行的bat在D的根目录下** Runtime r=Runtime.getRuntime();
File dir=new File("D:\\");
Process p=r.exec("cmd /c D:\\gg.bat",null,dir);
给你一个代码,可以解决这个问题。
Process ps = Runtime.getRuntime().exec("E:\\test.bat");
System.out.println(loadStream(ps.getInputStream())); //load the stream
System.out.println(loadStream(ps.getErrorStream()));//load the stream
//下面是读数据流的方法
static String loadStream(InputStream in) throws IOException
{
int ptr = 0;
in = new BufferedInputStream(in);
StringBuffer buffer = new StringBuffer();
while( (ptr = in.read()) != -1 )
{
buffer.append((char)ptr);
}
return buffer.toString();
}这样应该可以了
但是.bat 和.com文件呢?