本文介绍了Java Runtime.getRuntime()。exec失去/更改一些参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-Dexec.mainClass = maven.MainClass 成为 maven.MainClass 。)

在Java中使用maven执行Java类()
-Dexec.mainClass = maven.MainClass 成为 maven.MainClass

When executing Java class using maven from within Java (Eclipse plugin actualy)-Dexec.mainClass=maven.MainClass becomes maven.MainClass

package maven;

public class MainClass {
    public static void main(String[] args) {
        System.out.println("app is running!");
    }
}

从Eclipse使用

    Process p = DebugPlugin.exec(cmds, workingPath, envp);
    RuntimeProcess process = (RuntimeProcess)DebugPlugin.newProcess(launch, p, MavenConstants.PROCESS_MESSAGE);

p = Runtime.getRuntime()exec(cmdLine,envp) ; 只是调用Java

 p= Runtime.getRuntime().exec(cmdLine, envp);

连接的字符串数组看起来像下面的字符串,以某种方式失去了 -Dexec .mainClass = 部分。

And array of strings that concatenated looks like string below, somehow looses -Dexec.mainClass= part.

D:\Code\springsource\3.0\apache-maven-3.0.3\bin\mvn.bat compile exec:java -Dexec.mainClass=maven.MainClass 

输出

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building org.example.maven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.672s
[INFO] Finished at: Fri Jan 24 23:59:50 CST 2014
[INFO] Final Memory: 4M/15M
[INFO] ------------------------------------------------------------------------
[ERROR] Unknown lifecycle phase "maven.MainClass". You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-site, site, post-site, site-deploy, pre-clean, clean, post-clean. -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/LifecyclePhaseNotFoundException

但是从cmd shell使用 mvn 或完整的文件名可以

but from cmd shell using mvn or full filename is OK

D:\Progs\Enide-Monster-08-kepler-win32\runtime-EclipseApplication\org.example.maven>mvn compile exec:java -Dexec.mainClass=maven.MainClass
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building org.example.maven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ org.example.maven ---
[WARNING] Using platform encoding (Cp1251 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ org.example.maven ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ org.example.maven >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ org.example.maven <<<
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ org.example.maven ---
app is running!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.342s
[INFO] Finished at: Fri Jan 24 23:56:41 CST 2014
[INFO] Final Memory: 5M/15M
[INFO] ------------------------------------------------------------------------


D:\Progs\Enide-Monster-08-kepler-win32\runtime-EclipseApplication\org.example.maven>D:\Code\springsource\3.0\apache-maven-3.0.3\bin\mvn.bat compile exec:java -Dexec.mainClass=maven
.MainClass
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building org.example.maven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ org.example.maven ---
[WARNING] Using platform encoding (Cp1251 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ org.example.maven ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ org.example.maven >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ org.example.maven <<<
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ org.example.maven ---
app is running!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.435s
[INFO] Finished at: Fri Jan 24 23:57:46 CST 2014
[INFO] Final Memory: 5M/15M
[INFO] ------------------------------------------------------------------------


推荐答案

发现 cmdLine.add( - Dmaven.test.skip =真实); 也变成只有 true 有类似的错误。

Found that cmdLine.add("-Dmaven.test.skip=true"); also becomes just true with similar error.

我注意到没有在JavaDoc中提到这个行为的 Runtime.getRuntime()。exec(

I notices no mention in JavaDoc of this behavior for Runtime.getRuntime().exec(

通过将所有选项放在一个字符串。

Solved by putting all option in one string.

    cmdLine.add(mavenPath);
    cmdLine.add(mavenOptions+" compile exec:java -Dexec.mainClass="+packageClass);

这篇关于Java Runtime.getRuntime()。exec失去/更改一些参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 14:15