2014-06-18 54 views
0

我試圖從使用Apache公地EXEC Java應用程序中啓動腳本,得到以下錯誤:共享EXEC在環境變量的值尋找主類

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(); 
} 

該錯誤引發了我一個循環,因爲它似乎是將一個環境變量的引用值分開,並按該名稱查找類,而不是使用環境變量運行該命令。對於它的價值,下面在bash執行罰款:

JAVA_OPTS="-DappEnv=te -DsetInstance=true -Dinstance=.01" /path/to/bin/script.sh -s argVal 

任何人都可以提供一些見解,爲什麼帶引號的值是在空白分裂,和/或它爲什麼要找一個主類中的價值JAVA_OPTS?我是否正確使用環境地圖?

回答

0

問題的一部分是exec將其自己的引號添加到值JAVA_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()添加:

cmdLine.addArgument("-s"); 
cmdLine.addArgument("argVal");