Java:数据文件的相对路径
我已经在Mac(操作系统版本10.6.8)的NetBeans(7.2.1版)中开发了一个GUI应用程序.此应用程序需要一个XML文件(包含程序使用的数据),我希望将其放置在子目录data
中.
I have developed a GUI application in NetBeans (v. 7.2.1) on Mac (OS version 10.6.8). This application requires an XML file (containing data used by the program), which I would like to place in a subdirectory data
.
问题在于,如果我想从NetBeans中运行我的应用程序,则该数据目录必须放在主项目目录中;但是,如果我只想运行位于主目录dist
子目录中的.jar
文件(使用双击),则必须将数据文件夹移至dist
-然后该程序将不再从NetBeans内部运行.
The trouble is that if I want to run my application from within NetBeans, this data directory has to be placed inside the main project directory; however, if I want to simply run the .jar
file (using a doubleclick), located in the dist
subdir of the main dir, I have to move the data folder into dist
- and then the program will no longer run from within NetBeans.
这是在程序中指定数据文件路径的方式:
This is how the data file path is specified within the program:
public String vocabPath = "data" + File.separator + "wordlist.xml";
public File vocabFile = new File(vocabPath);
起初,这本身似乎没有问题,因为对于应用程序的重新分发,我认为我可以将数据目录简单地移至dist
并进行分发(在OS X和Windows 7上运行良好);但是,在Ubuntu上,我收到一条错误消息,提示未找到数据文件.
In itself at first this did not seem problematic, as, for redistribution of the application, I thought I could simply move the data directory into dist
, and distribute that (which worked fine on OS X and Windows 7); however, on Ubuntu I got an error message saying the data file was not found.
我是Java的新手,所以也许这是一个愚蠢的问题,但是无论如何:我如何才能让我的应用程序始终访问位于MAIN_DIR/dist/data
中的数据文件?或者,如果问题的根源在其他地方,请告诉我.
I am new to Java, so perhaps this is a daft question, but anyway: how can I get my application to always access the data file located in MAIN_DIR/dist/data
? Or if the root of the problem lies somewhere else, then please let me know.
非常感谢!
编辑
我发布了一个后续问题,因为我认为我已将问题缩小为一些意外行为,仅在Ubuntu下发生.
I have posted a follow-up question, as I think I have narrowed down the problem to some unexpected behaviour which only occurs under Ubuntu.
编辑2
我的后续问题的答案(请参阅第一个编辑)解决了我的问题(仅作为对那些试图解决与我的问题类似的问题的人的注释).
An answer to my follow-up question (see first edit) has solved my problem (just as a note to those who've found this post while trying to solve a problem similar to mine).
如果用户无法修改文件wordlist.xml
,则可以将其包含在jar中,并使用getResourceAsStream()
进行加载.其他一些详细信息,请参见 getResourceAsStream()vs FileInputStream
If the file wordlist.xml
is not modifiable by user, you could include it as part of jar and use getResourceAsStream()
to load it. Some additional details are here at getResourceAsStream() vs FileInputStream
如果希望修改数据,则可以将其保存在某个已知位置. 用户的主目录可通过user.home
属性获得. (这可能是个好地方)
if the data is expected to be modified , you may keep it at some known location. Users home directory is available via user.home
property. (and is probably a good area)
如果文件是由Java命令启动的纯Java程序,则拥有相对于安装"(MAIN_DIR)的文件会更加困难.您可以创建可执行文件,并使用某些操作系统api(在Linux上为readlink ("/proc/self/exe", ..)
)来解析安装位置.
Having a file relative to 'installation' (MAIN_DIR) is more difficult if it is a pure java program started by java command. You may create an executable and the use some operating system api (readlink ("/proc/self/exe", ..)
on Linux) to resolve the installation location.
使用本机可执行文件打包Java程序可以通过 exe4j href =一个>.
Packaging java program with native executable could be done by software like exe4j.
(Launch4J是具有类似功能的开源解决方案,在这里可能会有用)
(Launch4J is an opensource solution with similar features, could be useful here)
(为了完整起见和将来参考).您也可以尝试在Windows上创建一个小的批处理脚本,在Linux上创建一个shell脚本.可以从这两个位置检测安装区域.
(For the sake of completion and future reference). You could also try to create a small batch script on windows and shell script on Linux. It is possible to detect the installation area from both.
bat-请参见%〜dp0%-命令提示符中的目录导航. br> bash-从内部获取Bash脚本的源目录
bat - see %~dp0% - Directory navigation in command prompts.
bash - Getting the source directory of a Bash script from within