这可能是我在 Java 中见过的最奇怪的事情:以下代码(不使用外部库)使用 Ant 完美运行(在 Netbeans 上,在 Windows-7 机器上):
public static void main( String[] args ) throws IOException, InterruptedException
{
String prog = "\"C:\\Program Files\\R\\R-2.14.2\\bin\\Rscript.exe\"";
String[] progParams = new String[]{"C:\\Users\\pantelis\\Desktop\\SB\\MC_Sarfraz_ver5.R"};
Process p = Runtime.getRuntime().exec(prog+" "+progParams[0]);
p.waitFor();
System.out.println(p.exitValue()==0);
System.out.println("Done!!!");
}
它返回输出消息:
true
Done!!!
意思是顺利完成。当我尝试使用 Maven 运行相同的代码时,我没有遇到任何异常,但代码以:
false
Done!
并且该过程永远不会执行。这是我的 POM 文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>MVNTest</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MVNTest</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
更新 1:我使用以下代码打印该过程生成的任何错误消息:
String line;
BufferedReader input = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
这是输出:
Error in library(RDCOMClient) : there is no package called 'RDCOMClient'
Execution halted
仅当我使用 Maven 运行时才会出现此错误消息。
更新 2:当我使用 Maven 运行时,我使用 JDK7。下面是对应的命令:
cd C:\Users\pantelis\Documents\NetBeansProjects\MVNTest;
"JAVA_HOME=C:\\Program Files\\Java\\jdk1.7.0_04"
"\"C:\\Program Files\\NetBeans 7.1.2\\java\\maven\\bin\\mvn.bat\""
"-Dexec.args=-classpath %classpath com.mycompany.mvntest.App"
"-Dexec.executable=C:\\Program Files\\Java\\jdk1.7.0_04\\bin\\java.exe"
-Dexec.classpathScope=runtime process-classes
org.codehaus.mojo:exec-maven-plugin:1.2:exec
更新 3:这些是我使用 Ant 运行时得到的环境变量:
USERPROFILE -> C:\Users\pantelis
ProgramData -> C:\ProgramData
PATHEXT -> .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
Classpath -> C:\LINGO9\Lingo9_0.jar
ProgramFiles(x86) -> C:\Program Files (x86)
TEMP -> C:\Users\pantelis\AppData\Local\Temp
SystemDrive -> C:
ProgramFiles -> C:\Program Files
Path -> C:\Program Files (x86)\MiKTeX 2.9\miktex\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\WIDCOMM\Bluetooth Software\;C:\Program Files\WIDCOMM\Bluetooth Software\syswow64;\;C:\Program Files (x86)\Sony\VAIO Startup Setting Tool;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\LINGO9\;C:\Program Files (x86)\OpenVPN\bin
HOMEDRIVE -> C:
PROCESSOR_REVISION -> 2505
USERDOMAIN -> pantelis-VAIO
ALLUSERSPROFILE -> C:\ProgramData
ProgramW6432 -> C:\Program Files
PROCESSOR_IDENTIFIER -> Intel64 Family 6 Model 37 Stepping 5, GenuineIntel
SESSIONNAME -> Console
TMP -> C:\Users\pantelis\AppData\Local\Temp
CommonProgramFiles -> C:\Program Files\Common Files
=:: -> ::\
LOGONSERVER -> \\PANTELIS-VAIO
PROCESSOR_ARCHITECTURE -> AMD64
FP_NO_HOST_CHECK -> NO
OS -> Windows_NT
HOMEPATH -> \Users\pantelis
PROCESSOR_LEVEL -> 6
CommonProgramW6432 -> C:\Program Files\Common Files
configsetroot -> C:\Windows\ConfigSetRoot
LOCALAPPDATA -> C:\Users\pantelis\AppData\Local
COMPUTERNAME -> PANTELIS-VAIO
windir -> C:\Windows
SystemRoot -> C:\Windows
NUMBER_OF_PROCESSORS -> 4
USERNAME -> pantelis
PUBLIC -> C:\Users\Public
PSModulePath -> C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
CommonProgramFiles(x86) -> C:\Program Files (x86)\Common Files
ComSpec -> C:\Windows\system32\cmd.exe
APPDATA -> C:\Users\pantelis\AppData\Roaming
以下是我使用 Maven 运行时的环境变量:
USERPROFILE -> C:\Users\pantelis
JAVA_HOME -> C:\Program Files\Java\jdk1.7.0_04
PROGRAMDATA -> C:\ProgramData
MAVEN_CMD_LINE_ARGS -> "-Dexec.args=-classpath %classpath com.mycompany.mvntest.App" "-Dexec.executable=C:\Program Files\Java\jdk1.7.0_04\bin\java.exe" -Dexec.classpathScope=runtime process-classes org.codehaus.mojo:exec-maven-plugin:1.2:exec
CONFIGSETROOT -> C:\Windows\ConfigSetRoot
COMMONPROGRAMFILES -> C:\Program Files\Common Files
PROCESSOR_REVISION -> 2505
USERDOMAIN -> pantelis-VAIO
ALLUSERSPROFILE -> C:\ProgramData
PROGRAMW6432 -> C:\Program Files
SESSIONNAME -> Console
TMP -> C:\Users\pantelis\AppData\Local\Temp
PSMODULEPATH -> C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
NB_EXEC_MAVEN_PROCESS_UUID -> b5daa24f-51dc-48ac-ad6d-820dc245b80a
M2_HOME -> C:\Program Files\NetBeans 7.1.2\java\maven\bin\..
LOGONSERVER -> \\PANTELIS-VAIO
=:: -> ::\
PATH -> C:\Program Files (x86)\MiKTeX 2.9\miktex\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\WIDCOMM\Bluetooth Software\;C:\Program Files\WIDCOMM\Bluetooth Software\syswow64;\;C:\Program Files (x86)\Sony\VAIO Startup Setting Tool;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\LINGO9\;C:\Program Files (x86)\OpenVPN\bin
PROMPT -> $P$G
COMMONPROGRAMW6432 -> C:\Program Files\Common Files
PROCESSOR_LEVEL -> 6
LOCALAPPDATA -> C:\Users\pantelis\AppData\Local
COMPUTERNAME -> PANTELIS-VAIO
CLASSWORLDS_LAUNCHER -> org.codehaus.plexus.classworlds.launcher.Launcher
USERNAME -> pantelis
APPDATA -> C:\Users\pantelis\AppData\Roaming
WINDIR -> C:\Windows
PATHEXT -> .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PROGRAMFILES(X86) -> C:\Program Files (x86)
TEMP -> C:\Users\pantelis\AppData\Local\Temp
HOMEDRIVE -> C:
SYSTEMDRIVE -> C:
=C: -> C:\Users\pantelis\Documents\NetBeansProjects\MVNTest
ERROR_CODE -> 0
COMMONPROGRAMFILES(X86) -> C:\Program Files (x86)\Common Files
CLASSWORLDS_JAR -> "C:\Program Files\NetBeans 7.1.2\java\maven\bin\..\boot\plexus-classworlds-2.4.jar"
PROCESSOR_IDENTIFIER -> Intel64 Family 6 Model 37 Stepping 5, GenuineIntel
MAVEN_JAVA_EXE -> "C:\Program Files\Java\jdk1.7.0_04\bin\java.exe"
CLASSPATH -> C:\LINGO9\Lingo9_0.jar
PROCESSOR_ARCHITECTURE -> AMD64
FP_NO_HOST_CHECK -> NO
OS -> Windows_NT
HOMEPATH -> \Users\pantelis
COMSPEC -> C:\Windows\system32\cmd.exe
PROGRAMFILES -> C:\Program Files
HOME -> C:\Users\pantelis
NUMBER_OF_PROCESSORS -> 4
PUBLIC -> C:\Users\Public
SYSTEMROOT -> C:\Windows