【问题标题】:Running CMD commands from JAVA从 JAVA 运行 CMD 命令
【发布时间】:2013-04-30 16:15:06
【问题描述】:

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

【问题讨论】:

  • 根据 Java 约定,您应该以小写字符开头变量名。

标签: java cmd command


【解决方案1】:

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

【讨论】:

  • 是的,不,我只是将它折叠成一个我可以以管理员身份运行的 exe,然后它工作了,但无论如何谢谢
猜你喜欢
  • 1970-01-01
  • 2021-05-18
  • 1970-01-01
  • 1970-01-01
  • 2018-03-14
  • 1970-01-01
  • 2013-07-22
相关资源
最近更新 更多