You do not need shell script :)Sample:# ls -alF t -rwxr-xr-x 1 root root 20 Apr 3 09:49 t* # chown dureek.admin t # ls -alF t -rwxr-xr-x 1 dureek admin 20 Apr 3 09:49 t*
是呀,可以不用SHELL的,但Runtime类怎么用呢,给个例子吧。
import java.io.BufferedReader; import java.io.InputStreamReader;public class ExecTest { public static void main(String[] args) { StringBuffer cmd = new StringBuffer(); if ((args != null) && (args.length > 0)) { for (int i=0; i<args.length; i++) cmd.append(args[i]).append(' '); } else { System.out.println("You forgot to specify parameters ;]"); System.exit(1); } try { Process proc = Runtime.getRuntime().exec(cmd.toString()) ; BufferedReader br = new BufferedReader(new InputStreamRe ader(proc.getInputStream())); String s = null; while ((s = br.readLine()) != null) { System.out.println(s); } } catch (Exception e) { e.printStackTrace(); } } }
And try:# ls -alF ExecTest* -rw-r--r-- 1 root root 1269 May 29 10:58 ExecTest.class -rw-r--r-- 1 root root 781 May 29 10:58 ExecTest.java # java ExecTest /bin/chown nobody.nobody ExecTest.class # ls -alF -rw-r--r-- 1 nobody nobody 1269 May 29 10:58 ExecTest.class -rw-r--r-- 1 root root 781 May 29 10:58 ExecTest.java
麻烦Dureek,再写个Runtime在JSP中的例子。
Stop asking like a kid, do something meaning. I believe you can solve it perfectly by yourself =)* If these words made you feeling unhappy, I apologize ...
Dureek非常感谢你的帮助,能交个朋友吗?(EMAIL OR OICQ) MY OICQ:446065
# shell begin
chown myAccount $1
chgrp myAccount $1
# shell end其中$1就是命令行参数,这里应该是文件名。
如果要改变一个目录下所有文件的权限可以加上参数 -R
chown -R myAccount $1
chgrp -R myAccount $13.编辑以后用chmod +x shell_file_name 加上可执行权限
-rwxr-xr-x 1 root root 20 Apr 3 09:49 t*
# chown dureek.admin t
# ls -alF t
-rwxr-xr-x 1 dureek admin 20 Apr 3 09:49 t*
import java.io.InputStreamReader;public class ExecTest {
public static void main(String[] args) {
StringBuffer cmd = new StringBuffer(); if ((args != null) && (args.length > 0)) {
for (int i=0; i<args.length; i++)
cmd.append(args[i]).append(' ');
} else {
System.out.println("You forgot to specify parameters ;]");
System.exit(1);
} try {
Process proc = Runtime.getRuntime().exec(cmd.toString())
;
BufferedReader br = new BufferedReader(new InputStreamRe
ader(proc.getInputStream()));
String s = null;
while ((s = br.readLine()) != null) {
System.out.println(s);
}
} catch (Exception e) {
e.printStackTrace();
} }
}
-rw-r--r-- 1 root root 1269 May 29 10:58 ExecTest.class
-rw-r--r-- 1 root root 781 May 29 10:58 ExecTest.java
# java ExecTest /bin/chown nobody.nobody ExecTest.class
# ls -alF
-rw-r--r-- 1 nobody nobody 1269 May 29 10:58 ExecTest.class
-rw-r--r-- 1 root root 781 May 29 10:58 ExecTest.java
I believe you can solve it perfectly by yourself =)* If these words made you feeling unhappy, I apologize ...
MY OICQ:446065
mail: [email protected]