使用`Runtime.getRuntime()。exec`从Java启动的进程无法访问文件系统位置

时间:2014-01-26 07:43:33

标签: java eclipse maven gradle eclipse-rcp

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

如何确保已启动的流程具有相同的环境和权限?

更多详情:

mavengradle编写轻量级Eclipse插件。 (这个问题经过测试不会与Eclipse有关,最后会看到。)

我正在Eclipse插件中启动mvngradle

对于gradle gralde 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的{​​{1}}相同。虽然已经下载了所有必需的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

1 个答案:

答案 0 :(得分:0)

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

请参阅http://www.nodeclipse.org/projects/mavenhttp://www.nodeclipse.org/projects/gradle/