(Edited 9/19/2024 @ 10PM)
Code in Question
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]
I used to use commands like this before... this code is ment to make the JTextArea
(Image for refence - Imgur Link) To look similar to the console of the CMD screen/program. (Image for refence #1 - CMD Screen - Imgur Link & Image for reference #2 - Lookalike in GUI - Imgur Link)
And some code for parsing the returned lines to make it look neater. (though this is not a part of the error).
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.
And when trying to run program()
I get nothing returned...
I vary rarely am here to ask questions or in need of help, as most times i can find the answer, but I couldn't find the answer... so please help!
Adding the cmd /c
part into my code removed the error:
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
And when looking up why these commands no longer work with Process process = Runtime.getRuntime().exec(code)
I found this forum here del command with Runtime.exec() that said:
This is because del is a built in command of the command.com/cmd.exe shell. To execute such commands you have to invoke cmd.exe or command.com and pass it the del command as the argument.
However this was made 20 years ago, and unfortunatly I cannot find my projects that had cmd commands working without the cmd /c
part.
It's litterally the same commands I ran in the CMD version of the program:
: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
If this is a dumb question I am sorry.
To clarify the question, what do I do?
My code to run commands is not working, and/or giving a reply. So how do I fix this? When adding the cmd /c
I can see that it is doing something, but I have no clue what it's doing because I'm not getting a reply...
Unless the way I set up my code somehow does not give a reply because I have not used the correct way/proccess to get a reply. I'm not getting errors, but nor am I getting a result. It is quite literally doing nothing. If you look at the reference images, you will see that in Image for refence #1 - CMD Screen - Imgur Link the reply to the command:
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
It's supposed to give a reply similar to (example):
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.
The only thing I can think of is the return null
part of:
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;
Is somehow nullifying the other returns in the code. So again, what am I doing wrong?
See comments by 001 and life888888. If you return inside the loop, only first command is run. Instead you could save each run result in a list, or concatenate them:
for (int i=0; i<prg.length; i++) {
// concatenate or store return value in a list instead:
/*return*/ runCode(prg[i]);
}
However, it still won't work properly. You aren't seeing any error messages because you're not reading the STDERR stream of each process launched. For example, try use Microsoft Word to lock a file called "C:\Temp\a.docx", and then run this command to delete but with redirection of the out and error streams. It shows the error messages when trying to delete the locked file in a separate stream saved to 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.
The simplest way to capture STDERR stream in your code is to swap to ProcessBuilder
with option to merge STDERR -> STDOUT, and change input argument to runCode
to handle String[] cmdarray
not one string:
Process process = new ProcessBuilder(cmdarray)
.redirectErrorStream(true)
.start();
This avoids using deprecated call Process process = Runtime.getRuntime().exec(code)
.
You should use system variable "COMSPEC" to determine location of CMD.EXE, and note the other variables suggested in the comments by Mofi. Thus your commands should be:
String cmd = System.getenv("COMSPEC");
String local = System.getenv("LOCALAPPDATA");
String[][] prg = new String[][] {
// del is built-in to cmd so must be used via cmd:
{cmd, "/d", "/c", "del /q /f /s \""+local+"\\Temp\\*.*\""}
// preferably to prevent cmd AutoRun, use /d parameter too (See Mofi comment)
, {cmd, "/d", "/c", theCommand }
// Commands that are not built-in to cmd can be run without cmd if they are in the path
,{"ipconfig", "/FlushDNS"}
// or if not in the Path use absolute path (resolved using System.getenv if applicable)
};