[英]java issue when running ProcessBuilder - how to get the command to be executed from an external file?
I'm not an expert in java. 我不是Java方面的专家。
I'm trying to run some ms-dos commands via a java application. 我试图通过Java应用程序运行一些ms-dos命令。 everything works fine if I have the commands written in the java code.
如果我有用Java代码编写的命令,则一切正常。
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. 但是,如果我要将命令存储在输出“ .txt”文件中,请在执行过程中读取该文件,最后,通过Java运行命令,将无法正常工作。
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. 在这种情况下,结果与预期的一样,因为abc已在Java代码中填充。 however, if I get the content of abc from readbuffer (external file), it won't work.
但是,如果我从readbuffer(外部文件)中获取abc的内容,它将无法正常工作。
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. 我知道java的“ /”存在一些问题,有时会迫使我将其声明为“ //”,但是无论我在外部文件中有什么,它都将无法工作。
in the external ".txt" file, in order to populate abc, I have tried: 在外部“ .txt”文件中,为了填充abc,我尝试过:
"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. 不要像格式化Java代码那样尝试格式化或转义它。
.txt
files are not written in Java and don't use or respect Java syntax. .txt
文件不是用Java编写的,不使用或不遵守Java语法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.