I'm not an expert in java.
I'm trying to run some ms-dos commands via a java application. everything works fine if I have the commands written in the java code.
however, if i want to store the commands in an output ".txt" file, read this file during execution and, finally, run the commands via java, things won't work.
please, see the following code:
String abc = "cd \"C:\\Users\\Adm\" && dir";
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", abc);
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
...
in this case, the result is just like the expected because abc has been populated within the java code. however, if I get the content of abc from readbuffer (external file), it won't work.
what is the trick, please?
please note that I'm reading the external file properly. every line from the external file (each line contains a command) is being properly retrieved.
I know java has some problems with the "/", forcing me to declare it like "//" sometimes, but no matter what I have in the external file, it will not work.
in the external ".txt" file, in order to populate abc, I have tried:
"cd \"C:\\Users\\Adm\" && dir"
cd \"C:\\Users\" && dir
"cd \"C:\Users\Adm\" && dir"
cd \"C:\Users\" && dir
thanks for any help.
Just put the command you want to run in the file.
If you want to run:
cd "C:\Users\Adm" && dir
then that's what you should put in the file.
Don't try to format or escape it as if it was Java code. .txt
files are not written in Java and don't use or respect Java syntax.