繁体   English   中英

从JAVA运行CMD命令

[英]Running CMD commands from JAVA

我编写了此代码来移动文件夹,然后将其硬链接到其原始目的地。 当我只是从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();
    }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM