简体   繁体   English

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

[英]Running Windows Scanner in command prompt for a file From Java

I am trying to scan a file programmatically from Java and want to do it on the command line.我正在尝试从 Java 以编程方式扫描文件,并希望在命令行上执行此操作。

I have this working directly in the command line (simple navigate to folder and execute command:我直接在命令行中工作(简单导航到文件夹并执行命令:

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.

I want it to work from Java.我希望它从 Java 工作。 I am confused about The string that I should feed the Process .我对我应该为Process 提供的字符串感到困惑。 I have found some places that I should feed in the command strings with a \\c because it is widows.我发现有些地方应该用 \\c 输入命令字符串,因为它是寡妇。 But that doesn't work.但这不起作用。 Program is as follows.程序如下。 The String [] commands needs to be fixed. 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);
            }
        }
    }

Please help.请帮忙。

Some other combinations that I have tried for the command string:我为命令字符串尝试过的其他一些组合:

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

and

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\"\""
            }

Not sure how to break it up.不知道怎么破解。

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

This worked for me ^.这对我有用^。

I did not need the "cmd /c"我不需要“cmd / c”

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

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