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

2024-03-09

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

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

更多细节:

编写轻量级 Eclipse 插件maven https://github.com/Nodeclipse/nodeclipse-1/tree/master/org.nodeclipse.enide.maven and gradle https://github.com/Nodeclipse/nodeclipse-1/tree/master/org.nodeclipse.enide.gradle。 (这个问题经测试不是Eclipse相关见最后。)

我正在启动mvn and gradle从 Eclipse 插件中:

对于 gradlegralde build无法创建临时文件:

Eclipse 插件开发 - Gradle 无法从 Eclipse 启动器运行:java.io.IOException:访问被拒绝 https://stackoverflow.com/questions/21260657/eclipse-plugin-development-gradle-fails-to-run-from-eclipse-launcher-java-io

对于行家来说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外壳在哪里[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();
        }
    }
}

Output:

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

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用“Runtime.getRuntime().exec”从 Java 启动的进程无法访问文件系统位置 的相关文章

随机推荐

  • 为什么 http 服务返回 observables 而不是 Promise

    我对导致角度 2 的推理或讨论感到好奇http服务返回可观察的结果而不是响应的承诺 我会理解 Web 套接字连接或长轮询请求之类的逻辑 但是http服务只是创建一个XMLHttpRequest 并且在load请求的事件处理程序 在响应可观察
  • 在 PL/SQL 中创建或模拟二维数组

    您能帮助我如何在 PL SQL 中为存储过程创建二维数组吗 这些列是动态的 因此它的类型也可以增长和变化 任何帮助表示赞赏 先感谢您 我有以下代码 Type CAR TABLE ARRAY is varray 2 of varchar2 2
  • 更改 UITabBarItem 图像

    我已经使用 Storyboard 来设置我的UITabBarController及其对应的ViewControllers 每当取消选择选项卡时 它都是灰色的 选择它时 它会呈现绿色 我想要其中之一UITabBarItems始终看起来相同 即
  • Font-Face 使用绝对路径

    是否可以使用绝对路径 fontace 我有以下目录结构 HP font Monoton woff Monoton eot css font css fontface css html index html 定义字体 font face fo
  • 在 C++11 中使用零或 NULL 哪个更好?

    如今 有了C 11 推荐使用什么 Zero or NULL 第一个还是第二个如果 int p getPointer if 0 p something if NULL p something UPDATE 我忘记新的了 if nullptr
  • Entity Framework Core - 版本 5+ 与输出数据库无关

    我有一个使用 EF Core 的应用程序 最初是使用 V3 1 1 编写的 我们决定冒险更新到 v5 0 1 现在它已经不再预览了 该应用程序支持 MSSQL 和 SQLite 最初运行得很好 迁移的创建基本上与工具和文档无关here ht
  • 您的启动项目“项目”未引用 Microsoft.EntityFrameworkCore.Design

    我正在使用 EntityFrameworkCore 版本 2 2 2 构建 WPF 应用程序 然而 当我想要Add Migration它向我输出一个错误 Your startup project project doesn t refere
  • 如何配置星号即时消息

    星号支持即时消息吗 我尝试为 IM 配置星号 从这个例子 https groups google com forum msg csipsimple users stBJYGQ88fw OgLW8CLVvIkJ 但是当我尝试将 IM 发送到另
  • 下限 > 0 的 .Net 数组

    虽然这可能是一件奇怪的事情 但我需要在 Net 中创建一个下限 gt 0 的数组 乍一看 这似乎是可能的 使用 Array CreateInstance typeof Object new int 2 new int 9 产生所需的结果 下
  • 一个maven项目如何依赖另一个本地maven项目?

    我有两个 Maven 项目project foo pom xml and project bar pom xml 我有foo取决于bar and I want that every timefoo pom xmlcompiles it ll
  • 单元测试模拟 GCS

    我很难找到一种方法来进行单元测试read and write此类中存在的方法 我正在尝试使用模拟补丁库创建一个模拟 以避免调用 Google Storage 但我很难弄清楚如何做到这一点 from google cloud import s
  • 在c#中用0-9之间的随机数填充数组[重复]

    这个问题在这里已经有答案了 可能的重复 在 C 中用 0 9 之间的唯一随机数填充数组 https stackoverflow com questions 10688044 filling a array with uniqe random
  • 使用相关字段更新项目的示例过滤器挂钩

    我有 3 个集合 客户 经销商 和 订单 客户与经销商之间存在多对一的关系 经销商与客户之间是一对多的关系 订单与客户之间存在多对一的关系 我想使用先前配置的多对一关系根据所选客户自动设置orders reseller 字段 payload
  • img 元素上的 HTML 宽度/高度属性和 CSS 宽度/高度属性有什么区别?

    超文本标记语言 img 元素可以有width height属性 也可以有CSSwidth height特性 img src xxx img width 16 height 16 style width 16px height 16px HT
  • 我无法从数据库中检索图像

    我有一个包含电影名称 描述和封面图片的数据库 封面图片字段类型为 blob 问题是我无法从数据库中检索它 我想在他们旁边的封面图片上显示电影名称 如何做到这一点 这是我的代码 table border 1 width 100 tr td t
  • Perl 中的“字符串化”是什么?

    在 CPAN 模块的文档中DateTime http search cpan org drolsky DateTime 0 66 lib DateTime pm Formatters And Stringification我发现了以下内容
  • 在 Swift 中子类化 UINavigationBar

    我正在尝试创建一个自定义UINavigationBar类 然后使用Storyboard将其设置为我的班级UINavigationController的导航栏 这是我的代码UINavigationBar class class CustomN
  • 使用外部服务的 Shopify 自定义搜索

    我的情况 我们有一个 Shopify 商店 我们还有一个在 AWS 上实施和托管的搜索引擎 我们需要使用该搜索引擎而不是 Shopify 商店中的默认 search 理想情况下 当用户尝试搜索某些内容时 他们的查询 以及可能的其他内容 例如
  • C++ 静态变量总是在主线程上被破坏吗?

    简短的问题 C 11 静态 非线程本地 变量是否总是在主线程上分散注意力 它们是否总是仅在程序退出时被销毁 考虑到我们不手动调用它们的析构函数 UPDATE 为了简洁起见 我们假设调用了析构函数 我们没有拔掉插头 我们没有杀死 9 全局对象
  • 使用“Runtime.getRuntime().exec”从 Java 启动的进程无法访问文件系统位置

    从 Java 启动的进程Runtime getRuntime exec权限较少 我在 JavaDocs 中没有看到对此的描述以及如何管理它 如何保证启动的进程具有相同的环境和权限 更多细节 编写轻量级 Eclipse 插件maven htt