简体   繁体   English

从.jar Java中的当前目录搜索文件

[英]Search File from current directory in a .jar Java

I want to know how I can search a File on the System, but the File will be place in the same directory that the .jar. 我想知道如何在系统上搜索文件,但是该文件将与.jar放在同一目录中。 Example: 例:

MySearchProgram.jar (executable) MyFile.txt MySearchProgram.jar(可执行文件)MyFile.txt

Both files(MySearchProgram.jar, MyFile.txt) will be place in the same directory: /home/jfabian/Documents I need to read that file, to do that i need to Find it and specified the MyFile.txt directory, the problem is that I want to do it, from a .jar executable. 这两个文件(MySearchProgram.jar,MyFile.txt)将放置在同一目录中:/ home / jfabian / Documents我需要读取该文件,为此,我需要找到它并指定MyFile.txt目录,这是问题所在我想从.jar可执行文件执行此操作。

I know how to find a file, specifying the root, with this algorithm provide by Oracle: [Find.java][1] 我知道如何使用Oracle提供的以下算法查找指定根目录的文件:[Find.java] [1]

Thanks you in advance 提前谢谢你

To determine the directory where your JAR file file is located, use 要确定您的JAR文件文件所在的目录,请使用

Test.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()

on any class that was loaded from the JAR file ( Test in the example above). 从JAR文件加载的任何类上(在上面的示例中为Test )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM