从JAVA运行CMD命令

时间:2013-05-06 20:25:45

标签: java cmd command

我创建此代码以移动文件夹,然后将其硬链接到其原始目标。当我从eclipse尝试它时它完全有效的问题但是当我把它变成它自己的自动执行jar它不会创建硬链接但它会移动文件夹。代码运行命令行,然后输入命令。我不知道移动命令是否有效,而不是另一个。请帮忙。 (Mklink命令)

import java.io.*;
import javax.swing.JOptionPane;

public class The_Cloud_Setup {
    public static void main(String[] args) throws IOException
    {
        try {
            String command = "c:\\cmd.exe";
            Runtime.getRuntime().exec(command);
        }
        catch (IOException e){
            JOptionPane.showMessageDialog(null , e.getMessage(), "End Result", 2);
            System.err.println(e.getMessage());
        }
        String[] StringMove = { "cmd.exe", "/c", "move"+" "+"\"C:/Users/%username%/Documents/My Games/Terraria/Players\""+" "+"\"C:/Users/%username%/Google Drive/Players\""};
        String[] StringMklink = {"cmd.exe", "/c",  "mklink"+" "+"/d"+" "+"\"C:/Users/%username%/Documents/My Games/Terraria/Players\""+" "+"\"C:/Users/%username%/Google Drive/Players\""};
        Process ProcessMove = Runtime.getRuntime().exec(StringMove);
        Process ProcessMklink = Runtime.getRuntime().exec(StringMklink);
        BufferedReader VarMove = new BufferedReader(new InputStreamReader(ProcessMove.getInputStream()));
         BufferedReader VarMklink = new BufferedReader(new InputStreamReader(ProcessMklink.getInputStream()));
        String temp = "";
        while ((temp = VarMove.readLine()) != null) {
            System.out.println(temp);
        }
        VarMove.close();
        VarMklink.close();
    }
}

1 个答案:

答案 0 :(得分:0)

最有可能的是,在本机运行时,在程序尝试执行mklink命令之前,move命令尚未完成。您无法在存在现有文件夹的位置建立链接。