简体   繁体   English

Java:数据文件的相对路径

[英]Java: relative path for data file

I have developed a GUI application in NetBeans (v. 7.2.1) on Mac (OS version 10.6.8). 我已经在Mac(操作系统版本10.6.8)的NetBeans(版本7.2.1)中开发了一个GUI应用程序。 This application requires an XML file (containing data used by the program), which I would like to place in a subdirectory data . 此应用程序需要一个XML文件(包含程序使用的数据),我希望将其放置在子目录data

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; 麻烦在于,如果我想从NetBeans中运行应用程序,则该数据目录必须放在主项目目录中; 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. 但是,如果我只想运行(位于主目录的dist子目录中的) .jar文件(使用doubleclick),则必须将数据文件夹移至dist然后该程序将不再从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);

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); 首先,这本身似乎没有问题,因为对于应用程序的重新分发,我认为我可以将数据目录简单地移动到dist ,然后将其分发(在OS X和Windows 7上运行良好)。 however, on Ubuntu I got an error message saying the data file was not found. 但是,在Ubuntu上,我收到一条错误消息,提示未找到数据文件。

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 ? 我是Java新手,所以也许这是一个愚蠢的问题,但是无论如何:我如何才能让我的应用程序始终访问位于MAIN_DIR/dist/data的数据文件? Or if the root of the problem lies somewhere else, then please let me know. 或者,如果问题的根源在其他地方,请告诉我。

Thanks a lot in advance! 在此先多谢!

EDIT 编辑

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. 我已经发布了一个后续问题 ,因为我认为我已将问题缩小到某些仅在Ubuntu下发生的意外行为。

EDIT 2 编辑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). 我的后续问题的答案(请参阅第一个编辑)已经解决了我的问题(仅作为对那些试图解决与我的问题类似的发现该帖子的人的说明)。

If the file wordlist.xml is not modifiable by user, you could include it as part of jar and use getResourceAsStream() to load it. 如果用户无法修改wordlist.xml文件,则可以将其包含在jar中,并使用getResourceAsStream()进行加载。 Some additional details are here at getResourceAsStream() vs FileInputStream 一些额外的细节在这里getResourceAsStream()vs FileInputStream

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. 用户主目录可通过user.home属性获得。 (and is probably a good area) (可能是一个好地方)

Having a file relative to 'installation' (MAIN_DIR) is more difficult if it is a pure java program started by java command. 如果它是一个由Java命令启动的纯Java程序,则拥有相对于“安装”(MAIN_DIR)的文件会更加困难。 You may create an executable and the use some operating system api ( readlink ("/proc/self/exe", ..) on Linux) to resolve the installation location. 您可以创建可执行文件,并使用某些操作系统api(在Linux上为readlink ("/proc/self/exe", ..) )来解析安装位置。

Packaging java program with native executable could be done by software like exe4j . 用本机可执行文件打包Java程序可以通过exe4j之类的软件来完成。

(Launch4J is an opensource solution with similar features, could be useful here) (Launch4J是具有类似功能的开源解决方案,在这里可能很有用)

(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. 您也可以尝试在Windows上创建一个小的批处理脚本,在Linux上创建一个shell脚本。 It is possible to detect the installation area from both. 可以从两个位置检测安装区域。

bat - see %~dp0% - Directory navigation in command prompts . bat-请参见%〜dp0%- 命令提示符中的目录导航
bash - Getting the source directory of a Bash script from within bash- 从内部获取Bash脚本的源目录

I would have kept a System Property for the file and keep the path of dist by default. 我将为文件保留一个系统属性,并默认保留dist的路径。 In case file is present in data which is Development environment the application would pick it up otherwise it will always go to pick up file for dist . 如果文件存在于开发环境的data ,则应用程序将对其进行拾取,否则它将始终为dist选择文件。

You can do like: 您可以像这样:

// This would pick up system property if set otherwise default value provided.
String filePath = (String) System.getProperty("VOCAB_FILE_PATH","<<path with dist>>"); 
public File vocabFile = new File(filePath);

In you NetBeans environment just set a system property VOCAB_FILE_PATH with path mentioned as data . 在您的NetBeans环境中,只需设置一个系统属性VOCAB_FILE_PATH其中提到的路径就是data

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

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