Processes Launched from Java with `Runtime.getRuntime().exec` can't get access to file system places

ε祈祈猫儿з 提交于 2019-12-01 11:57:59

问题


Process Launched from Java with Runtime.getRuntime().exec has less permissions and I don't see description for that in JavaDocs and how to manage that.

How to ensure that launched process has the same environment and permissions?

More details:

Writing lightweight Eclipse plugins for maven and gradle. (This problem is tested not to be Eclipse related see in the end.)

I am launching mvn and gradle from within Eclipse plugin:

For gradle gralde build fails to create a temporary file:

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

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

The line above is different from the same launch from cmd shell where it is [DEBUG] Resolved plugin prefix exec to org.codehaus.mojo:exec-maven-plugin from repository central. It is the same every time though all required jar and metadata are already downloaded.
Log continues:

[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

After this line there is likely write exception, because this download goes every time. Maven reports higher level error, that it cannot get needed artifacts for compile exec, though (saying once again) they are already in local maven repo:

[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

UPDATE: Made sure it is not Eclipse related. Following Java code produces similar log output as above

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();
        }
    }
}

Output:

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

回答1:


Solved for Maven and Gradle by passing a set of environment variables.

See http://www.nodeclipse.org/projects/maven and http://www.nodeclipse.org/projects/gradle/



来源:https://stackoverflow.com/questions/21360995/processes-launched-from-java-with-runtime-getruntime-exec-cant-get-access-t

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