gpt4 book ai didi

Java执行文件相对于jar

转载 作者:行者123 更新时间:2023-11-30 07:54:22 25 4
gpt4 key购买 nike

我有一个 C# 控制台应用程序,我将使用 Java 发送推荐,使用此处描述的技术 Sending commands to a console application?

我的问题是控制台应用程序位于与 jar 位于同一文件夹中的文件夹中,并且我无法找到如何执行相对于 jar 所在目录的文件。

我在 Java 中尝试了很多东西,比如

Process pr = Runtime.getRuntime().exec("runtime_libraries/consoleapp.exe");

但它说找不到该文件。

java.io.IOException: CreateProcess error=2, The system cannot find the file specified

我以为这很简单,但显然事实并非如此。

所以我的问题是,如何将相对路径放入运行时执行中?

最佳答案

当您遇到此类问题时,您需要做一些初步检查...

  1. 使用 System.out.println(System.getProperty("user.dir")); 验证您当前的运行位置,并确保它位于您期望的位置;
  2. 使用 System.out.println(new File("runtime_libraries/consoleapp.exe").exists())); 验证 consoleapp.exe 是否存在

大多数 IDE 允许您在开发过程中更改工作目录。在生产时,应确保工作目录设置正确(例如,在Windows下,可以将快捷方式配置为在指定位置“启动”)

关于Java执行文件相对于jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32879343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com