简体   繁体   English

运行ProcessBuilder时出现Java问题-如何从外部文件获取要执行的命令?

[英]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.

相关问题 从Java中的ProcessBuilder执行时,Windows REG命令不起作用 - Windows REG command not working when executed from ProcessBuilder in Java 在Java中使用ProcessBuilder执行外部命令时如何提高性能 - How to increase performance when using ProcessBuilder to execute external command in Java 从终端执行的命令,使用ProcessBuilder从Java失败 - Command executed from terminal, failed from Java using ProcessBuilder Java ProcessBuilder如何从命令获取二进制输出 - Java ProcessBuilder how to get binary output from command 当用ProcessBuilder执行Java时,为什么Java无法打开批处理文件? - Why will Java not open up a Batch file when executed with a ProcessBuilder? 如何设置Java ProcessBuilder参数以运行外部.java文件? - How to set Java ProcessBuilder parameter to run external .java file? 从命令行执行Java程序时文件未写入 - File not writing when java program is executed from command line Java 从 ProcessBuilder 获取 PID - Java get PID from ProcessBuilder 授予文件权限并在Java Servlet中运行ProcessBuilder - Giving File permissions and running ProcessBuilder in Java Servlets 在Java中使用ProcessBuilder执行命令时出现IllegalArgumentException - IllegalArgumentException when executing command with ProcessBuilder in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM