java - 在命令提示符下运行 Windows 扫描程序以获取来自 Java 的文件

标签 java windows cmd command-prompt processbuilder

我正在尝试从 Java 以编程方式扫描文件,并希望在命令行上执行此操作。

我可以直接在命令行中使用此功能(简单导航到文件夹并执行命令:

c:\Users\3XXXXX8\Desktop>cd "C:\\Program Files\\Windows Defender" && MpCmdRun.exe -Scan -ScanType 3 -File "C:\\UploadedFiles\\file"
Scan starting...
Scan finished.
Scanning C:\\UploadedFiles\\file found no threats.

我希望它能在 Java 上运行。 我对应该提供给进程的字符串感到困惑。我发现有些地方应该用\c 输入命令字符串,因为它是寡妇。但这是行不通的。程序如下。 String[] 命令需要修复。

import java.io.*;

    public class CmdTest {
        public static void main(String[] args) throws Exception {
            String [] commands = {
                "cmd /c \"cd \"C:\\Program Files\\Windows Defender\" && MpCmdRun.exe -Scan -ScanType 3 -File \"C:\\UploadedFiles\\file\"\""
            }
            ProcessBuilder builder = new ProcessBuilder(commands);
            builder.redirectErrorStream(true);
            Process p = builder.start();
            BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ( (line = r.readLine()) != null) {
                System.out.println(line);
            }
        }
    }

请帮忙。

我尝试过的命令字符串的其他一些组合:

String [] commands = { "cmd.exe", "/c", "cd \"C:\\Program Files\\Windows Security Client\"",
                    "MpCmdRun.exe -Scan -ScanType 3 -File C:\\UploadedFiles\\" + file.getName()
            };

String [] commands = {
            "cmd /c \"cd \"C:\\Program Files\\Windows Defender\" && MpCmdRun.exe -Scan -ScanType 3 -File \"C:\\UploadedFiles\\file\"\""
        }


String [] commands = {
                "cmd \"cd \"C:\\Program Files\\Windows Defender\" && MpCmdRun.exe -Scan -ScanType 3 -File \"C:\\UploadedFiles\\file\"\""
            }

不知道如何分解它。

最佳答案

String [] commands = {
            "\"C:\\Program Files\\Windows Defender\\MpCmdRun.exe\" -Scan -ScanType 3 -File \"C:\\UploadedFiles\\file\""
        };

这对我有用^。

我不需要“cmd/c”

关于java - 在命令提示符下运行 Windows 扫描程序以获取来自 Java 的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60084997/

相关文章:

batch-file - 到 "Call"或 "Not to Call"一个批处理文件?

java - 如何使用 printf 将单独的字符串格式化为一行?

java - 不从 ArrayList 中删除对象

java - JSP 声明 scriptlet 访问 bean

java - 从可比数组中删除项目

Windows PowerShell : changing the command prompt

windows - 批处理文件不工作 : Spaces in path

windows - 如何使用 CMD 命令将文件路径拆分为其组件,并使用双反斜杠再次连接

windows - 在 emacs 中重新加载 Clojure 文件

windows - 扩展命令提示符的环境变量