Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Commons exec在环境变量值中查找主类_Java_Environment Variables_Apache Commons Exec - Fatal编程技术网

Java Commons exec在环境变量值中查找主类

Java Commons exec在环境变量值中查找主类,java,environment-variables,apache-commons-exec,Java,Environment Variables,Apache Commons Exec,我正在尝试使用Apache commons exec从Java应用程序中启动脚本,并出现以下错误: Error: Could not find or load main class "-DappEnv=te org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1) at org.apache.commons.exec.DefaultExecutor.executeIn

我正在尝试使用Apache commons exec从Java应用程序中启动脚本,并出现以下错误:

Error: Could not find or load main class "-DappEnv=te
org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
    at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:402)
    at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:164)
    at TestRunner.runTest(TestRunner.java:37)
    at TestMain.main(TestMain.java:6)
对于以下代码:

String jOpts = "JAVA_OPTS=\"-DappEnv=te -DsetInstance=true -Dinstance=.01\"";
String command = "/path/to/bin/script.sh -s argVal";
try {
  Map<String, String> procEnv = EnvironmentUtils.getProcEnvironment();
  EnvironmentUtils.addVariableToEnvironment(procEnv, jOpts);
  CommandLine cmdLine = CommandLine.parse(command);
  DefaultExecutor executor = new DefaultExecutor();
  executor.setWorkingDirectory(new File("/path/to"));
  executor.execute(cmdLine, procEnv);
} catch (Exception e) {
  e.printStackTrace();
}

有谁能提供一些见解,解释为什么引用的值被拆分为空白,和/或为什么它在
JAVA\u OPTS
的值中寻找一个主类?我是否正确使用了环境映射?

问题的一部分是exec在
JAVA\u OPTS的值中添加了自己的引号。如果该值周围没有引号,则环境变量将设置为精细:

String jOpts = "JAVA_OPTS=-DappEnv=te -DsetInstance=true -Dinstance=.01";
该命令的格式也不正确。传递给
commandLine.parse()
的参数应该只是要运行的程序的名称:

String command = "/path/to/bin/script.sh";
CommandLine cmdLine = CommandLine.parse(command);
其余的参数需要添加到
addArgument()

String command = "/path/to/bin/script.sh";
CommandLine cmdLine = CommandLine.parse(command);
cmdLine.addArgument("-s");
cmdLine.addArgument("argVal");