Code for Ant not Running in Maven

一个人想着一个人 提交于 2019-12-10 18:52:51

问题


This is probably the most strange thing I have ever seen in Java: The following code (that uses no external libraries) runs flawlessly using Ant (on Netbeans, on a Windows-7 Machine) :

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!!!"); 
}

and it returns the output message:

true
Done!!!

meaning that it completed smoothly. When I'm trying to run the very same code using Maven, I get no exceptions but the code finishes with:

false
Done!

and the process is never executed. This is my POM file:

<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>

Update 1: I used the following code to print any error messages generated by the process:

String line;
BufferedReader input = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((line = input.readLine()) != null) {
  System.out.println(line);
}

Here is the output:

Error in library(RDCOMClient) : there is no package called 'RDCOMClient'
Execution halted

This error message appears only when I run using Maven.

Update 2: When I run using Maven, I use JDK7. Here is the corresponding command:

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

Update 3: These are the environmental variables that I get when I run with 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

And here are the environmental variables when I run with 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

回答1:


After the update, based on the error you get when running from maven, it seems some (class)path is not set up correctly: RDCOMClient seems to be related to R, so it is the setup that is used to run Rscript.exe that is the problem.

Print out the environmental variables when you are running via ant and also under maven and make a comparison to determine the source of the missing library error

Update from comments:

The problem might be related to how the project is executed from maven: exec:exec starts a new JVM, whereas exec:java runs the project in the same JVM that is running maven.

Update from comments:

Apparently, the "Compile on Save" settings in NetBeans change the maven goal from exec:exec to exec:java. Alternatively, you can configure the pom.xml manually (see the exec maven plugin page that has examples and descriptions for the two goals)




回答2:


Does this happen with all R packages ? Or only RDCOMClient ?

I'll be very surprised if this is a Maven/Ant issue.

Reading the error stream is unnecessary. R outputs the error in a file or you can use sink("filename") to write R output to a file.

Please treat as comment.




回答3:


I think, you need to specify your prog and params inside an array, and pass that array to exec.

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[] {
     prog,
     "C:\\Users\\pantelis\\Desktop\\SB\\MC_Sarfraz_ver5.R"
  };
  Process p = Runtime.getRuntime().exec(progParams);
  p.waitFor();
  System.out.println(p.exitValue()==0);
  System.out.println("Done!!!"); 
}


来源:https://stackoverflow.com/questions/10363119/code-for-ant-not-running-in-maven

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!