繁体   English   中英

通过java在后台运行cmd命令

[英]Run cmd commands in the background through java

我正在用java创建一个GUI,它需要在用户选择一个文件并单击GUI中的一个按钮后,在后台一个接一个地通过命令行运行一组命令。 要执行的命令将包括文件的路径。 用户选择按钮后,如何使用一组命令在后台运行命令行?

到目前为止,GUI 仅包含文件选择器。

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;

public class GUIProject {

    public static void main(String[] args) {
        JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
        int returnValue = jfc.showOpenDialog(null);
        // int returnValue = jfc.showSaveDialog(null);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File selectedFile = jfc.getSelectedFile();
            System.out.println(selectedFile.getAbsolutePath()); //Instead of printing send it to cmd and perform more commands 
        }
    }
}

你可以试试这个:

String command = "cd C:\users";
Process p = Runtime.getRuntime().exec(command);

对于多个命令,您可以使用&&运算符

Runtime.getRuntime().exec("cmd /c \"start somefile.bat && start other.bat && cd C:\\test && test.exe\"");

暂无
暂无

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

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