gpt4 book ai didi

java - Ant 的代码未在 Maven 中运行

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:34:00 26 4
gpt4 key购买 nike

这可能是我在 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

最佳答案

更新后,根据从 maven 运行时出现的错误,似乎某些(类)路径设置不正确:RDCOMClient 似乎与 R 相关,所以它是用于运行 Rscript 的设置.exe 就是问题所在。

打印出ant运行时和maven下运行时的环境变量,并进行对比,判断缺失库错误的来源

评论更新:

问题可能与项目如何从 maven 执行有关:exec:exec 启动一个新的 JVM,而 exec:java 在同一个 JVM 中运行项目正在运行 maven。

评论更新:

显然,NetBeans 中的“保存时编译”设置将 Maven 目标从 exec:exec 更改为 exec:java。或者,您可以手动配置 pom.xml(请参阅 exec maven plugin 页面,其中包含两个目标的示例和描述)

关于java - Ant 的代码未在 Maven 中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10363119/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com