当前位置: 技术问答>java相关
关于JAVA运行SHELL程序的问题!
来源: 互联网 发布时间:2015-01-01
本文导语: 这个程序能够编译,却不能执行,为什么?谢谢指点! import java.net.InetAddress; import java.io.*; import java.util.*; public class test { public static void main(){ try{ Runtime myRun = Runtime.getRuntime(); Process myProc = myRun.exec("cp -R ...
这个程序能够编译,却不能执行,为什么?谢谢指点!
import java.net.InetAddress;
import java.io.*;
import java.util.*;
public class test
{
public static void main(){
try{
Runtime myRun = Runtime.getRuntime();
Process myProc = myRun.exec("cp -R /usr/local/site/etc /home/test");
int retcode = myProc.waitFor();
}
catch( Exception e)
{
}
}
}
import java.net.InetAddress;
import java.io.*;
import java.util.*;
public class test
{
public static void main(){
try{
Runtime myRun = Runtime.getRuntime();
Process myProc = myRun.exec("cp -R /usr/local/site/etc /home/test");
int retcode = myProc.waitFor();
}
catch( Exception e)
{
}
}
}
|
看这个行不行。
===================
import java.io.*;
public class CmdExec {
public CmdExec(String cmdline) {
try {
String line;
Process p = Runtime.getRuntime().exec(cmdline);
// jdk1.0.2
DataInputStream input =
new DataInputStream(p.getInputStream());
// jdk1.1.1
// BufferedReader commandResult =
// new BufferedReader
// (new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
}
catch (Exception err) {
System.out.println("EXEC failed: " + err.toString());
err.printStackTrace();
}
}
public static void main(String argv[]) {
/*
** javac CmdExec myapp.exe
** javac CmdExec "myapp.exe /a"
*/
new CmdExec(argv[0]);
}
}
==================
public class Test {
public static void main(String[] args) throws Exception {
Process p = Runtime.getRuntime().exec(
""c:/program files/windows/notepad.exe"");
p.waitFor();
}
}
===================
import java.io.*;
public class CmdExec {
public CmdExec(String cmdline) {
try {
String line;
Process p = Runtime.getRuntime().exec(cmdline);
// jdk1.0.2
DataInputStream input =
new DataInputStream(p.getInputStream());
// jdk1.1.1
// BufferedReader commandResult =
// new BufferedReader
// (new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
}
catch (Exception err) {
System.out.println("EXEC failed: " + err.toString());
err.printStackTrace();
}
}
public static void main(String argv[]) {
/*
** javac CmdExec myapp.exe
** javac CmdExec "myapp.exe /a"
*/
new CmdExec(argv[0]);
}
}
==================
public class Test {
public static void main(String[] args) throws Exception {
Process p = Runtime.getRuntime().exec(
""c:/program files/windows/notepad.exe"");
p.waitFor();
}
}