1。Process process1 = Runtime.getRuntime().exec("cmd /c copy c:\\a.doc d:\\b.doc");
2。Process process2 = Runtime.getRuntime().exec("start d:\\b.doc");
运行总是发生错误,说没有b.doc文件,原因可能是1需要较长的时间,而2急着要运行,所以发生错误。改成如下,程序就死了。
1。Process process1 = Runtime.getRuntime().exec("cmd /c copy c:\\a.doc d:\\b.doc");
process1.waitFor();
2。Process process2 = Runtime.getRuntime().exec("start d:\\b.doc");
process2.waitFor();
2。Process process2 = Runtime.getRuntime().exec("start d:\\b.doc");
运行总是发生错误,说没有b.doc文件,原因可能是1需要较长的时间,而2急着要运行,所以发生错误。改成如下,程序就死了。
1。Process process1 = Runtime.getRuntime().exec("cmd /c copy c:\\a.doc d:\\b.doc");
process1.waitFor();
2。Process process2 = Runtime.getRuntime().exec("start d:\\b.doc");
process2.waitFor();
如果是:那下面的代码可以实现
public class tt
{
public static void main(String args[])
{
try
{
Process process1 = Runtime.getRuntime().exec("cmd /c start copy c:\\c.doc d:\\a.doc");
Process process2 = Runtime.getRuntime().exec("cmd /c start d:\\a.doc");
}
catch(Exception e){System.out.println ("fdsfsd");}
}}
public class tt
{
public static void main(String args[])
{
try
{
Process process1 = Runtime.getRuntime().exec("cmd /c start c:\\c.doc");
Process process2 = Runtime.getRuntime().exec("cmd /c start d:\\a.doc");
}
catch(Exception e){System.out.println ("fdsfsd");}
}}二条代码我都运行通过.给分吧100分
我的就不会死
也就是复制命令还没执行完,打开操作就要进行,当然出错了。
加上waitfor也不行。
import java.util.*;
import java.io.*;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class recursion
{
public static void main(String[] args) throws IOException , InterruptedException
{
Process process1,process2;
process1 = Runtime.getRuntime().exec("cmd /c copy c:\\a.txt d:\\b.txt");
//process1.waitFor();
if (process1.waitFor() == 0)
process2 = Runtime.getRuntime().exec("cmd /c d:\\b.txt");
}
}