【问题标题】:Processes Launched from Java with `Runtime.getRuntime().exec` can't get access to file system places使用“Runtime.getRuntime().exec”从 Java 启动的进程无法访问文件系统位置
【发布时间】:2014-02-17 02:51:22
【问题描述】:

使用Runtime.getRuntime().exec 从Java 启动的进程具有较少的权限 我在 JavaDocs 中没有看到关于它的描述以及如何管理它。

如何保证启动的进程具有相同的环境和权限?

更多细节:

mavengradle 编写轻量级Eclipse 插件。 (这个问题经测试不是Eclipse相关见最后。)

我正在 Eclipse 插件中启动 mvngradle

对于gradlegralde build创建临时文件失败:

Eclipse plugin development - Gradle fails to run from Eclipse launcher: java.io.IOException: Access denied

对于 maven D:\Progs\springsource\apache-maven-3.0.4\bin\mvn.bat -X compile exec:java -Dexec.mainClass=runclass.Runme

Apache Maven 3.0.4 (r1232337; 2012-01-17 16:44:56+0800)
Maven home: D:\Progs\springsource\apache-maven-3.0.4
Java version: 1.7.0_11, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.7.0_11\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
[INFO] Error stacktraces are turned on.
[DEBUG] Reading global settings from D:\Progs\springsource\apache-maven-3.0.4\conf\settings.xml
[DEBUG] Reading user settings from C:\Users\weibl\.m2\settings.xml
[DEBUG] Using local repository at C:\Users\weibl\.m2\repository
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10 for C:\Users\weibl\.m2\repository
[INFO] Scanning for projects...
[DEBUG] Extension realms for project maven-example:maven-example:jar:0.0.1-SNAPSHOT: (none)
[DEBUG] Looking up lifecyle mappings for packaging jar from ClassRealm[plexus.core, parent: null]
[DEBUG] Resolving plugin prefix  compile exec from [org.apache.maven.plugins, org.codehaus.mojo]
[DEBUG] Could not find metadata org.apache.maven.plugins/maven-metadata.xml in local (C:\Users\weibl\.m2\repository)

上面的行与cmd shell 的相同启动不同,它是[DEBUG] Resolved plugin prefix exec to org.codehaus.mojo:exec-maven-plugin from repository central。尽管已经下载了所有必需的 jar 和元数据,但每次都是一样的。
日志继续:

[DEBUG] Skipped remote update check for org.apache.maven.plugins/maven-metadata.xml, locally cached metadata up-to-date.
[DEBUG] Could not find metadata org.codehaus.mojo/maven-metadata.xml in local (C:\Users\weibl\.m2\repository)
[DEBUG] Skipped remote update check for org.codehaus.mojo/maven-metadata.xml, locally cached metadata up-to-date.
[DEBUG] Could not find metadata org.apache.maven.plugins/maven-metadata.xml in local (C:\Users\weibl\.m2\repository)
[DEBUG] Could not find metadata org.codehaus.mojo/maven-metadata.xml in local (C:\Users\weibl\.m2\repository)
[DEBUG] Using connector WagonRepositoryConnector with priority 0 for http://repo.maven.apache.org/maven2
[DEBUG] Using connector WagonRepositoryConnector with priority 0 for http://repo.maven.apache.org/maven2
Downloading: http://repo.maven.apache.org/maven2/org/codehaus/mojo/maven-metadata.xml
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-metadata.xml
3/13 KB   
7/13 KB   
10/13 KB   
3/22 KB   10/13 KB   
7/22 KB   10/13 KB   
11/22 KB   10/13 KB   
11/22 KB   12/13 KB   
15/22 KB   12/13 KB   
19/22 KB   12/13 KB   
22/22 KB   12/13 KB   

Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/mojo/maven-metadata.xml (22 KB at 15.0 KB/sec)
[DEBUG] Writing resolution tracking file C:\Users\weibl\.m2\repository\org\codehaus\mojo\resolver-status.properties
13/13 KB              

Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-metadata.xml (13 KB at 6.1 KB/sec)
[DEBUG] Writing resolution tracking file C:\Users\weibl\.m2\repository\org\apache\maven\plugins\resolver-status.properties

在这一行之后可能会出现写入异常,因为每次下载都会进行。 Maven 报告更高级别的错误,它无法获得compile exec 所需的工件, 虽然(再说一遍)他们已经在本地 Maven 仓库中:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.655s
[INFO] Finished at: Sun Jan 26 13:51:56 CST 2014
[INFO] Final Memory: 8M/152M
[INFO] ------------------------------------------------------------------------
[ERROR] No plugin found for prefix ' compile exec' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (C:\Users\weibl\.m2\repository), central (http://repo.maven.apache.org/maven2)] -> [Help 1]
org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException: No plugin found for prefix ' compile exec' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (C:\Users\weibl\.m2\repository), central (http://repo.maven.apache.org/maven2)]
    at org.apache.maven.plugin.prefix.internal.DefaultPluginPrefixResolver.resolve(DefaultPluginPrefixResolver.java:94)
    at org.apache.maven.lifecycle.internal.MojoDescriptorCreator.findPluginForPrefix(MojoDescriptorCreator.java:262)
    at org.apache.maven.lifecycle.internal.MojoDescriptorCreator.getMojoDescriptor(MojoDescriptorCreator.java:222)
    at org.apache.maven.lifecycle.internal.DefaultLifecycleTaskSegmentCalculator.calculateTaskSegments(DefaultLifecycleTaskSegmentCalculator.java:106)
    at org.apache.maven.lifecycle.internal.DefaultLifecycleTaskSegmentCalculator.calculateTaskSegments(DefaultLifecycleTaskSegmentCalculator.java:86)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:98)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
[ERROR] 
[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/NoPluginFoundForPrefixException

更新:确保它与 Eclipse 无关。以下 Java 代码产生与上述类似的日志输出

import java.util.*;
import java.io.*;

// http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html
public class MediocreExecJavac {
    private static void log(String message) {
        System.out.println(message);
    }
    public static void main(String args[]) {
        try {

            String mavenPath = "D:\\Progs\\springsource\\apache-maven-3.0.4\\bin\\mvn.bat";
            String mavenOptions  = "-X compile exec:java -Dexec.mainClass=runclass.Runme";

            String[] cmdLine = new String[2];
            cmdLine[0] = mavenPath;  //cmdLine.add(mavenPath);
            cmdLine[1] = mavenOptions;      //cmdLine.add(mavenOptions+" compile exec:java -Dexec.mainClass="+packageClass);        

            String[] envp = new String[2];
            //Map<String, String> envm = new HashMap<String, String>();
            envp[0] = "JAVA_HOME=" + System.getProperty("java.home"); //System.getenv("JAVA_HOME");
            envp[1] = "M2_HOME=" + System.getenv("MAVEN_HOME");     

            File workingDirectory = null;
            String currentDir = new File(".").getAbsolutePath();
            log(currentDir);
            String userDir = System.getProperty("user.dir"); //User working directory ; "user.home"     User home directory
            workingDirectory = new File(userDir);       
            log(workingDirectory.toString());

            //
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec(cmdLine, envp, workingDirectory);
            InputStream stdout = proc.getInputStream();
            InputStream stderr = proc.getErrorStream();
            InputStreamReader isr = new InputStreamReader(stdout);
            InputStreamReader isr2 = new InputStreamReader(stderr);
            BufferedReader br = new BufferedReader(isr);
            BufferedReader br2 = new BufferedReader(isr2);

            String line = null;
            System.out.println("<OutputStream>");
            while ((line = br.readLine()) != null)
                System.out.println(line);
            System.out.println("</OutputStream>");

            System.out.println("<ErrorStream>");
            while ((line = br2.readLine()) != null)
                System.out.println(line);
            System.out.println("</ErrorStream>");

            int exitVal = proc.waitFor();
            System.out.println("Process exitValue: " + exitVal);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}

输出:

...
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoPluginFoundForPrefixException
</OutputStream>
<ErrorStream>
'cmd' �����ڲ����ⲿ���Ҳ���ǿ����еij���
���������
</ErrorStream>
Process exitValue: 1

【问题讨论】:

  • 您是否尝试过在没有 Eclipse 等的情况下从命令行运行 maven 命令?
  • 是的,请阅读问题的结尾。有代码示例。
  • 错误plugin found for prefix ' compile exec' 看起来很奇怪。就像测试一样-您可以尝试单独下载maven并设置M2_HOME变量-重命名%USERPROFILE%\.m2目录然后重新运行。在 Windows Vista+(包括 7 和 8)中,当您启动一个进程时,它始终以较少的权限运行,除非您强制它以管理员身份运行。
  • 所有在 Windows 7 上的 cmd shell 中都能正常工作。错误 No plugin found for prefix ' compile exec' 并不准确,因为在该阶段实际上存在 IO 错误。我已经在日志部分之间添加了对此的评论。请参阅正确报告的 Gradle stackoverflow.com/questions/21260657/…

标签: java eclipse maven gradle eclipse-rcp


【解决方案1】:

通过传递一组环境变量解决了 Maven 和 Gradle。

http://www.nodeclipse.org/projects/mavenhttp://www.nodeclipse.org/projects/gradle/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-15
    • 2013-11-10
    • 2011-03-10
    • 2013-01-21
    • 2014-02-21
    • 1970-01-01
    • 2012-05-23
    • 1970-01-01
    相关资源
    最近更新 更多