eclipse包结构中的路径解析
对于许多读到这篇文章的人来说,这是一个非常简单的问题,但对我来说却很新鲜。 这是我的 Eclipse 的屏幕截图
当我运行此程序时,我得到 java.io.FileNotFoundException :querys.xml(系统找不到指定的文件)
我尝试了../../../queries.xml
但这也不起作用。我真的不明白何时使用 ../
因为这意味着在 dir 中后退 1 步,并且在某些情况下它有效,有人可以解释一下吗?另外我如何在这里引用querys.xml。谢谢
注意:我什至可以在 Linux 机器上使用此代码
This is a very simple question for many of you reading this, but it's quite new for me.
Here is a screenshot for my eclipse
When i run this program i get java.io.FileNotFoundException: queries.xml (The system cannot find the file specified)
i tried ../../../queries.xml
but that is also not working. I really don't understand when to use ../
because it means go 1 step back in dir, and in some cases it works, can anyone explain this? Also how can I refer to queries.xml here. Thanks
Note: I might even use this code on a linux box
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设它将您的代码编译到
build
或classes
文件夹中,并从那里运行它...您是否尝试过传统的 Java 方法来执行此操作:
假设构建步骤是将 xml 复制到构建文件夹中,我相信应该可以工作
,但我不使用 Eclipse,所以不能 100% 确定...
I assume it is compiling your code into a
build
orclasses
folder, and running it from there...Have you tried the traditional Java way for doing this:
Assuming the build step is copying the xml into the build folder, I believe that should work
I don't use Eclipse though, so can't be 100% sure...
尝试
检查您可以使用的 eclipse 的实际工作目录
Try
To check the actual working directory of eclipse you can use
您可以尝试使用属性文件来存储 xml 文件的路径。
这样您就可以将 xml 文件放置在任何位置,并且只需更改属性文件即可。
这不需要更改/重新编译代码。
这意味着您只需要对属性文件的路径进行硬编码。
如果您不喜欢对属性文件的路径进行硬编码,则可以在启动期间将其作为参数传递到服务器设置文件中。 (在 tomcat web.xml 中)。每个服务器都会有一个等效的设置文件,您可以在其中指定属性文件的路径。
或者,如果您不想使用属性文件,则可以在同一文件中指定 xml 的路径。
此链接将向您展示读取属性文件的示例。
http://www.zparacha.com/how-to-读取属性文件-in-java/
You could try using a property file to store the path of the xml files.
This way you can place the xml files in any location, and simply change the property file.
This will not require a change/recompilation of code.
This would mean you will only need to hardcode the path of the property file.
If you prefer not hardcoding the path of the property file, then you could pass it as an argument during startup in your server setup file. (in tomcat web.xml). Every server will have an equivalent setup file where you could specify the path of the property file.
Alternatively you could specify the path of the xml in this same file, if you don't want to use property files.
This link will show you an example of reading from property files.
http://www.zparacha.com/how-to-read-properties-file-in-java/