exec,文件位置问题

发布于 2024-12-03 21:49:44 字数 1188 浏览 0 评论 0原文

在我的 jar 应用程序中,我在 exe 程序中做了一些计算。当文件和program.exe位于同一目录时,我使用了这个命令:

String[] str={"program.exe", "file1.txt", "file2.txt"};
pr = rt.exec(str);

并且效果很好。但是当我将文件移动到其他目录并尝试使用此命令时:

String[] str={"program.exe", "temp\\file1.txt", "temp\\file2.txt"};
pr = rt.exec(str);

program.exe 看不到文件。更奇怪的是,当我将其名称更改为其他默认值时,它开始看到文件。 file1.txt、file2.txt和temp是在program.exe启动之前在我的jar程序中创建的。

编辑:
当问题开始时,我尝试这样的操作:默认名称 file1.txt 和 file2.txt,我更改为 aaa.txt 和 bbb.txt (在 Windows 中),然后:

 String[] str={"program.exe", "temp\\aaa.txt", "temp\\bbb.txt"};

它起作用了。

编辑2:
现在我知道问题出在program.exe 中。当我从命令行(而不是从 jar )使用它时,如下所示:

program.exe temp\file1.txt temp\file2.txt 

错误:

FANN Error 1: Unable to open configuration file "temp\file1.txtÉ║@" for reading.

fann 是人工神经网络库。当我将文件复制到 program.exe 目录时:

program.exe file1.txt file2.txt 

它有效!当我更改 temp 中的文件名并执行以下操作时:

program.exe temp\file1aaa.txt temp\file2bbb.txt

它也有效!那么这是 fann lib bug 吗?

In my jar application I do some calculations in exe program. When files and program.exe was in the same dir I used this command:

String[] str={"program.exe", "file1.txt", "file2.txt"};
pr = rt.exec(str);

and it worked great. But when I moved files to other dir and I try use this command:

String[] str={"program.exe", "temp\\file1.txt", "temp\\file2.txt"};
pr = rt.exec(str);

program.exe doesn't see files. What is more odd it start to see files when I change its names for anything else that default. file1.txt, file2.txt and temp are created in my jar program before program.exe start.

edit:
When problem started I try sth like this: default names file1.txt and file2.txt, I changed to aaa.txt and bbb.txt (in windows), and then:

 String[] str={"program.exe", "temp\\aaa.txt", "temp\\bbb.txt"};

and it works.

edit2:
Now I know that problem is in program.exe. When I use it from command line (not from jar), like this:

program.exe temp\file1.txt temp\file2.txt 

error:

FANN Error 1: Unable to open configuration file "temp\file1.txtÉ║@" for reading.

fann is artificial neural network library. When I copy files to program.exe dir:

program.exe file1.txt file2.txt 

it works! When I changed files names in temp and do:

program.exe temp\file1aaa.txt temp\file2bbb.txt

it works also! So it is fann lib bug?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扎心 2024-12-10 21:49:44

我会使用 ProcessBuilder api(它为您提供比 Runtime.exec() 更多的控制权),并且我还会使用绝对路径:

File directory = new File("/path/tp/program.exe's/parent");

int returnCode = new ProcessBuilder("program.exe", 
    new File(directory, "temp/file1.txt").getAbsolutePath(),
    new File(directory, "temp/file2.txt").getAbsolutePath()
)
.directory(directory).start().waitFor();

I'd use the ProcessBuilder api (it gives you much more control than Runtime.exec()) and I'd also use absolute paths:

File directory = new File("/path/tp/program.exe's/parent");

int returnCode = new ProcessBuilder("program.exe", 
    new File(directory, "temp/file1.txt").getAbsolutePath(),
    new File(directory, "temp/file2.txt").getAbsolutePath()
)
.directory(directory).start().waitFor();
能怎样 2024-12-10 21:49:44

给出文件名的完整路径并查看。如下所示

String[] str = {"program.exe", "D:\\temp\\file1.txt", "D:\\temp\\file2.txt"}; 

如果您的操作系统是基于 UNIX 的,则进行相应的更改。

Give complete path of the filename and see. Something like below

String[] str = {"program.exe", "D:\\temp\\file1.txt", "D:\\temp\\file2.txt"}; 

If your OS is UNIX based then change it accordingly.

旧人哭 2024-12-10 21:49:44

您是否尝试过相对路径来查找位置
喜欢
<代码>
abc(文件夹)
 ->代码(文件夹)
  -->Program.Java
 ->温度
  -->文件1.txt
所以
当你在 Eclipse IDE 中运行你的程序时

您的相对路径将来自program.java 文件是
../temp/file1.txt
并尝试使用 / 而不是 \,这样它就不会被视为转义字符。

当你从罐子里跑出来时
您需要将 jar 中的 temp 文件夹解压到外部

abc(文件夹)
 -> jar(文件夹)
  -->Program.jar
 ->温度
  --> file1.txt


从程序中以 zip 文件形式读取 jar 内容。通过代码到达其中的临时文件夹,然后将内容作为输入流读取。

Have you tried relative path for finding the location
like

abc (folder)
 -> code(folder)
  -->Program.Java
 -> temp
  --> file1.txt

so
when u run ur program in Eclipse IDE

ur relative path will be from program.java file is
../temp/file1.txt
And try to use / instead of \ so that it wont take as an escape character.

when u run from a jar
You need to extract the temp folder from jar to outside

abc (folder)
 -> jar (folder)
  -->Program.jar
 -> temp
  --> file1.txt

OR
Read the jar content from the program as a zip file. Reach your temp folder inside it by code and then read the content as input stream.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文