(编辑于 9/19/2024 @ 10PM)
有问题的代码
public static String runCode(String code) {
String ret="";
try {
Process process = Runtime.getRuntime().exec(code);
BufferedReader reader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
line+=line;
}
int exitCode = process.waitFor();
ret=line;
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return ret;
}
public static String program() {
String[] G=new String[] {
"A","B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
"L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
"W", "X", "Y", "Z"};
String[] prg = new String[] {
"cmd /c del /q /f /s \"%USERPROFILE%\\AppData\\Local\\Temp\\*.*\"",
"cmd /c del /q /f /s \"%USERPROFILE%\\AppData\\Local\\Microsoft\\Windows\\"
+ "Temporary Internet Files\\*.*\"",
"cmd /c del /q /f /s \"%USERPROFILE%\\AppData\\Local\\Temp\\Excel8.0\\*.exd”",
"cmd /c del /q /f /s \"%USERPROFILE%\\AppData\\Roaming\\Microsoft\\Office\\*.tmp\"",
"cmd /c del /q /f /s \"%SystemRoot%\\prefetch\\*.*\"",
"cmd /c ipconfig /FlushDNS"};
for (int i=0; i<prg.length; i++) {
return runCode(prg[i]);
}
for (int i=0; i<G.length; i++) {
return runCode("cmd /c IF EXIST "+G[i]+":\\$RECYCLE.BIN\\S-1-5-21-3255149829-"
+ "3976585364-830875472-1001\\*\" del \"%%G:\\$RECYCLE.BIN\\S-1-"
+ "5-21-3255149829-3976585364-830875472-1001\\*\" /q /f /s");
}
return null;
}
[some more code down here but currently not causing issues]
我以前曾经使用过这样的命令...这段代码是为了使
JTextArea
(参考图片 - Imgur Link)看起来类似于 CMD 屏幕/程序的控制台。 (参考图片 #1 - CMD 屏幕 - Imgur 链接 & 参考图片 #2 - GUI 中的相似 - Imgur 链接)
还有一些用于解析返回行的代码,使其看起来更整洁。 (尽管这不是错误的一部分)。
Note: A:\Users\UltraGaming\Documents\NetBeansProjects\Internet Cleaning Program\src\Main.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
当尝试运行时
program()
我没有得到任何回报......
我很少在这里提出问题或需要帮助,因为大多数时候我可以找到答案,但我找不到答案......所以请帮忙!
将
cmd /c
部分添加到我的代码中删除了错误:
java.io.IOException: Cannot run program "del": CreateProcess error=2, The system cannot find the file specified
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1143)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1073)
at java.base/java.lang.Runtime.exec(Runtime.java:615)
at java.base/java.lang.Runtime.exec(Runtime.java:439)
at java.base/java.lang.Runtime.exec(Runtime.java:322)
at Main.runCode(Main.java:68)
at Main.program(Main.java:101)
at Main.<init>(Main.java:34)
at Main.Start(Main.java:18)
at Main.main(Main.java:14)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.base/java.lang.ProcessImpl.create(Native Method)
at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:494)
at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:159)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1110)
... 9 more
当查找为什么这些命令不再适用于
Process process = Runtime.getRuntime().exec(code)
时,我在这里找到了这个论坛 del 命令与 Runtime.exec() 说:
这是因为 del 是 command.com/cmd.exe shell 的内置命令。要执行此类命令,您必须调用 cmd.exe 或 command.com 并将 del 命令作为参数传递给它。
然而这是 20 年前制作的,不幸的是我找不到我的项目,它的 cmd 命令在没有
cmd /c
部分的情况下也能工作。
这与我在程序的 CMD 版本中运行的命令几乎相同:
:start
cls
echo INTERNET CLEANING PROGRAM HAS BEEN STARTED!!!
echo The following will not alter nor delete your files...
echo This program may cause some downloads to fail!
echo.
echo.
del /q /f /s %TEMP%\*
goto forloop
:forloop
FOR %%G IN (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO IF EXIST "%%G:\$RECYCLE.BIN\S-1-5-21-3255149829-3976585364-830875472-1001\*" del "%%G:\$RECYCLE.BIN\S-1-5-21-3255149829-3976585364-830875472-1001\*" /q /f /s
goto continue
:continue
del /q /f /s "%USERPROFILE%\AppData\Local\Temp\*.*"
del /q /f /s "%USERPROFILE%\AppData\Local\Microsoft\Windows\Temporary Internet Files\*.*"
del /q /f /s "%USERPROFILE%\AppData\Local\Temp\Excel8.0\*.exd”
del /q /f /s "%USERPROFILE%\AppData\Roaming\Microsoft\Office\*.tmp"
del /q /f /s "%SystemRoot%\prefetch\*.*"
ipconfig /FlushDNS
timeout 45
goto start
如果这是一个愚蠢的问题,我很抱歉。
为了澄清这个问题,我该怎么办?
我的运行命令的代码不起作用,和/或给出答复。那么我该如何解决这个问题呢?添加
cmd /c
时,我可以看到它正在做某事,但我不知道它在做什么,因为我没有得到回复...
除非我设置代码的方式不知何故没有给出答复,因为我没有使用正确的方式/过程来获得答复。我没有收到错误,但也没有得到结果。从字面上看,它实际上什么也没做。如果您查看参考图像,您将在参考图像#1 - CMD 屏幕 - Imgur 链接中看到对命令的回复:
del /q /f /s "%USERPROFILE%\AppData\Local\Temp\*.*"
del /q /f /s "%USERPROFILE%\AppData\Local\Microsoft\Windows\Temporary Internet Files\*.*"
del /q /f /s "%USERPROFILE%\AppData\Local\Temp\Excel8.0\*.exd”
del /q /f /s "%USERPROFILE%\AppData\Roaming\Microsoft\Office\*.tmp"
del /q /f /s "%SystemRoot%\prefetch\*.*"
ipconfig /FlushDNS
它应该给出类似于(示例)的回复:
The system cannot find the path specified.
C:\Users\UltraGaming\AppData\Local\Temp\3d469667-2a94-4b44-bef0-fb0c6760513c.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\4659658e-314c-4764-85f4-516ce16390b6.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\54b12231-4611-45c6-93f3-e05ea46153b6.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\6e093a4e-7556-4241-abf2-9b86c054d390.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\70b02fbc-2e92-4ac8-a2a9-9298fd5c9e62.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\71859995-8655-40f9-9ec5-0ba3579c4679.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\7d339d05-aeb9-4e3e-b09c-b3199dad7544.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\862b49e7-4ebe-456b-aa4a-a7b65a7e950c.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\88472edf-231e-4704-b2f1-75dd0cf48ff3.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\8fa7aea4-8659-483f-92d9-2ab634b21df7.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\9a6049e9-f5c8-4e28-b19d-81de0c3b24fe.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\9e937077-7fa5-4103-a628-edda0ec39b8c.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\a7de362c-53af-4558-ae43-7dfe61901bed.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\c1554335-9aa3-4e6e-aa43-d0710d83dc06.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\c93ff693-7863-4842-9c97-ded1daeb80ff.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\d56279ec-798b-4ff8-8bec-33e418c1de7b.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\e80c1750-c549-437f-b02e-32dd063589d4.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\output1726766868491
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\output1726778059006
The process cannot access the file because it is being used by another process.
Could Not Find D:\$RECYCLE.BIN\S-1-5-21-3255149829-3976585364-830875472-1001\*
C:\Users\UltraGaming\AppData\Local\Temp\3d469667-2a94-4b44-bef0-fb0c6760513c.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\4659658e-314c-4764-85f4-516ce16390b6.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\54b12231-4611-45c6-93f3-e05ea46153b6.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\6e093a4e-7556-4241-abf2-9b86c054d390.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\70b02fbc-2e92-4ac8-a2a9-9298fd5c9e62.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\71859995-8655-40f9-9ec5-0ba3579c4679.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\7d339d05-aeb9-4e3e-b09c-b3199dad7544.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\862b49e7-4ebe-456b-aa4a-a7b65a7e950c.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\88472edf-231e-4704-b2f1-75dd0cf48ff3.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\8fa7aea4-8659-483f-92d9-2ab634b21df7.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\9a6049e9-f5c8-4e28-b19d-81de0c3b24fe.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\9e937077-7fa5-4103-a628-edda0ec39b8c.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\a7de362c-53af-4558-ae43-7dfe61901bed.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\c1554335-9aa3-4e6e-aa43-d0710d83dc06.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\c93ff693-7863-4842-9c97-ded1daeb80ff.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\d56279ec-798b-4ff8-8bec-33e418c1de7b.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\e80c1750-c549-437f-b02e-32dd063589d4.tmp
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\output1726766868491
The process cannot access the file because it is being used by another process.
C:\Users\UltraGaming\AppData\Local\Temp\output1726778059006
The process cannot access the file because it is being used by another process.
The system cannot find the file specified.
Could Not Find C:\Users\UltraGaming\AppData\Roaming\Microsoft\Office\*.tmp
Deleted file - C:\WINDOWS\prefetch\CHROME1.EXE-8C6899DA.pf
Deleted file - C:\WINDOWS\prefetch\DLLHOST.EXE-15EEAC72.pf
Deleted file - C:\WINDOWS\prefetch\DLLHOST.EXE-60B0F5A5.pf
Deleted file - C:\WINDOWS\prefetch\DLLHOST.EXE-9B6FC840.pf
Deleted file - C:\WINDOWS\prefetch\DLLHOST.EXE-CD45FFE9.pf
Deleted file - C:\WINDOWS\prefetch\FENCESTASKBARITEM.EXE-9786ED35.pf
Deleted file - C:\WINDOWS\prefetch\IPCONFIG.EXE-912F3D5B.pf
Deleted file - C:\WINDOWS\prefetch\NOTEPAD++.EXE-3F73AA45.pf
Deleted file - C:\WINDOWS\prefetch\TIMEOUT.EXE-902DED03.pf
Windows IP Configuration
Successfully flushed the DNS Resolver Cache.
The system cannot find the path specified.
我唯一能想到的是
return null
部分:
for (int i=0; i<prg.length; i++) {
return runCode(prg[i]);
}
for (int i=0; i<G.length; i++) {
return runCode("cmd /c IF EXIST "+G[i]+":\\$RECYCLE.BIN\\S-1-5-21-3255149829-"
+ "3976585364-830875472-1001\\*\" del \"%%G:\\$RECYCLE.BIN\\S-1-"
+ "5-21-3255149829-3976585364-830875472-1001\\*\" /q /f /s");
}
return null;
以某种方式使代码中的其他返回无效。再说一遍,我做错了什么?
参见001和life888888的评论。如果返回循环内部,则仅运行第一个命令。相反,您可以将每个运行结果保存在列表中,或将它们连接起来:
for (int i=0; i<prg.length; i++) {
// concatenate or store return value in a list instead:
/*return*/ runCode(prg[i]);
}
不过我还是不会工作。您没有看到任何错误消息,因为您没有读取启动的每个进程的 STDERR 流。例如,尝试使用 Microsoft Word 锁定名为“C:\Temp .docx”的文件,然后运行此命令进行删除,但重定向输出流和错误流。当尝试删除保存到 stderr.log 的单独流中的锁定文件时,它会显示错误消息:
> del /q /f /s C:\Temp\a.docx > stdout.log 2> stderr.log
> type stdout.log
C:\Temp\a.docx
> type stderr.log
The process cannot access the file because it is being used by another process.
在代码中捕获 STDERR 流的最简单方法是交换到
ProcessBuilder
,并选择合并 STDERR -> STDOUT,并将输入参数更改为 runCode
以处理 String[] cmdarray
不是一个字符串:
Process process = new ProcessBuilder(cmdarray)
.redirectErrorStream(true)
.start();
这可以避免使用已弃用的调用
Process process = Runtime.getRuntime().exec(code)
。
您应该使用“COMSPEC”来确定 CMD.EXE 的位置,并注意注释中建议的其他系统变量。因此你的命令应该是:
String com = System.getenv("COMSPEC");
String local = System.getenv("LOCALAPPDATA");
String[][] prg = new String[][] {
{cmd, "/c", "del /q /f /s \""+local+"\\Temp\\*.*\""}
...
,{cmd, "/c", "ipconfig /FlushDNS"}
};