在Java中混淆命令行执行

在Java中混淆命令行执行,java,Java,最近,我在Java应用程序中执行命令行时遇到了一些问题。当我执行命令时,一切都很顺利 然后创建一个特定的目录。然而,当我想做一些更复杂的事情时,事情并不顺利 为什么会这样?正常的一个短语命令进入,另一个不进入?传递给exec方法的字符串将自动解析以定义命令的参数。因为您的路径包含空格(并且可能也包含特殊字符),所以您应该考虑使用构造命令。 此外,ProcessBuilder的构造函数是要执行的命令,但您也可以使用该方法更改工作目录 如果不想检查命令的退出值(在Windows中,退出值是一个名为e

最近,我在Java应用程序中执行命令行时遇到了一些问题。当我执行命令时,一切都很顺利

然后创建一个特定的目录。然而,当我想做一些更复杂的事情时,事情并不顺利


为什么会这样?正常的一个短语命令进入,另一个不进入?

传递给
exec
方法的字符串将自动解析以定义命令的参数。因为您的路径包含空格(并且可能也包含特殊字符),所以您应该考虑使用构造命令。

此外,ProcessBuilder的构造函数是要执行的命令,但您也可以使用该方法更改工作目录

如果不想检查命令的退出值(在Windows中,退出值是一个名为
errorlevel
的伪环境变量,如中所述),可以执行以下操作:

try {
    String[] cmd = {"cmd", 
                    "/c", 
                    "gradlew", 
                    "assembleRelease"};
    ProcessBuilder pb = new ProcessBuilder(cmd);
    // Change working directory
    pb.directory(new File("C:\\Users\\CA_LTD\\AndroidStudioProjects\\AMBITION"));
    // Run command
    Process p = pb.start();
    // Wait for completion
    p.waitFor();
} catch (IOException ioe) {
    ioe.printStackTrace();
}

所以在@CristianRamon Cortes发布解决方案后,我得到了以下输出:

FAILURE: Build failed with an exception.
Starting a Gradle Daemon, 1 incompatible and 1 stopped Daemons could not be reused, use --status for details
NDK is missing a "platforms" directory.
If you are using NDK, verify the ndk.dir is set to a valid NDK directory.  It is currently set to C:\Users\CA_LTD\AppData\Local\Android\Sdk\ndk-bundle.
If you are not using NDK, unset the NDK variable from ANDROID_NDK_HOME or local.properties to remove this warning.

Incremental java compilation is an incubating feature.

BUILD FAILED

Total time: 29.414 secs

* What went wrong:
Task '' not found in root project 'AMBITION'. Some candidates are: ''.

* Try:
Run gradlew tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Exception in thread "main" java.lang.Exception: ERROR: CMD process exited with non-zero value: 1
    at com.example.test.TestingCmd.main(TestingCmd.java:43)
编辑:见鬼,这个词中只有一个字母“m”。
谢谢你@CristianRamon Cortes

你面临什么问题?出现了什么错误?可能是因为文件夹名中有空格,并且Exc假设最后两个单词是分开的PARAMS?考虑使用命令代替单个字符串,这样您就可以真正地将哪个值传递给每个参数。我不知道您希望用CMD命令执行什么,所以您应该考虑使用不同的CMD定义。通过使用该命令创建该项目。什么是非零退出值?我为什么要定义我的行为?代码是:int exitValue=p.waitFor();//如果(exitValue!=0)检查退出值{//TODO:定义进程以非零退出值退出时抛出新异常的行为(“错误:CMD进程以非零值退出:“+exitValue”);}真的需要吗?如果我有非零退出值,我不知道该怎么办。你的代码不起作用。可能是因为这个原因,我有一个非零出口值的异常。如果您只使用控制台,您将执行哪个命令?exit值是命令的返回代码(可以使用名为errorlevel的伪环境变量进行检查)。我将在不检查退出值的情况下添加一个版本。它的退出值不为零,并且不起作用。那我该怎么办?
FAILURE: Build failed with an exception.
Starting a Gradle Daemon, 1 incompatible and 1 stopped Daemons could not be reused, use --status for details
NDK is missing a "platforms" directory.
If you are using NDK, verify the ndk.dir is set to a valid NDK directory.  It is currently set to C:\Users\CA_LTD\AppData\Local\Android\Sdk\ndk-bundle.
If you are not using NDK, unset the NDK variable from ANDROID_NDK_HOME or local.properties to remove this warning.

Incremental java compilation is an incubating feature.

BUILD FAILED

Total time: 29.414 secs

* What went wrong:
Task '' not found in root project 'AMBITION'. Some candidates are: ''.

* Try:
Run gradlew tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Exception in thread "main" java.lang.Exception: ERROR: CMD process exited with non-zero value: 1
    at com.example.test.TestingCmd.main(TestingCmd.java:43)