jsp里调用sh文件是否可以调

我在我的jsp里写了这样一句
java.lang.Runtime.getRuntime().exec(path+"ts.sh");
是不是就调用了这个ts。sh脚本文件

试了一下发现报
cannot execute

[quote]引用
看看api
java.lang.Runtime.getRuntime().exec(" ");

Executes the specified string command in a separate process.

里面执行的是命令,不是指定文件名。

exec的参数可以是文件名的
Java代码
Runtime.getRuntime()

.exec("C:\Program Files\Mozilla Firefox\firefox.exe");

直接打开可执行程序
在Linux下,可以指定执行的shell脚本,如
Java代码
Runtime.getRuntime().exec("test.sh");

不过我是在SSH终端下执行的,没看到test.sh中执行的效果。[/quote]

这个是api文档,exec接受的是一条命令
你的这条语句也是执行的一条命令而已
Runtime.getRuntime()

.exec("C:\Program Files\Mozilla Firefox\firefox.exe");

Process java.lang.Runtime.exec(String command) throws IOException

Executes the specified string command in a separate process.

This is a convenience method. An invocation of the form exec(command) behaves in exactly the same way as the invocation exec(command, null, null).

Parameters:
command a specified system command.
Returns:
A new Process object for managing the subprocess

理论上只要在命令行里面能执行的命令,exec应该都是可以执行的。
那个sh文件,可能用sh test.sh或者./test.sh能执行,我没有试。
返回一个Process类型的变量,可以对其操作。

好像隐约的记得,windows下没法执行.sh的Shell脚本吧。
你用这代码去linux下试试。Linux应该可以这样执行。

看看api
java.lang.Runtime.getRuntime().exec(" ");

Executes the specified string command in a separate process.

里面执行的是命令,不是指定文件名。

应该是可以的,JSP最终也是被编译成Servlet的,还是后台调用,肯定是可以的。

[quote]看看api
java.lang.Runtime.getRuntime().exec(" ");

Executes the specified string command in a separate process.

里面执行的是命令,不是指定文件名。[/quote]

exec的参数可以是文件名的
[code="java"] Runtime.getRuntime()
.exec("C:\Program Files\Mozilla Firefox\firefox.exe");[/code]
直接打开可执行程序
在Linux下,可以指定执行的shell脚本,如[code="java"]Runtime.getRuntime().exec("test.sh");[/code]

不过我是在SSH终端下执行的,没看到test.sh中执行的效果。

在linux下是能执行的,这种情况先打印path+"ts.sh",最好使用绝对路径

调用方法如下:

[code="java"]String[] cmd = new String[] { "/bin/sh", "-c", path+"ts.sh"};

Process process = Runtime.getRuntime().exec(cmd);[/code]

如果还没有执行,那么需要写一个线程,在线程中及时把输入流和错误流中的信息都走,要不会阻塞!

楼主可以参考我的一片文章:(调用windows 下的命令)

[url]http://walsh.iteye.com/blog/450055[/url]

没必要切换目录,直接将路径设置为classpath执行不就行了。

假如你的类在当前目录的 test下,你就这么细写:
java.lang.Runtime.getRuntime().exec("java -cp ./test 包名.Test");

就可以了

错误信息需要到其返回的Process里面去取。。。。