问题描述
最近,我遇到了Java问题.我已经尝试了一些在网上找到的东西,但是没有用,所以我需要帮助.我在Eclipse中有一个Java项目.我的主要课程在src/programCode/UI_Main2.java
中.在该.java
中,我尝试访问src/files/File.file
Recently I have an issue with Java. I've tried some things I found on the web but they haven't worked, so I need help.I have a Java project in Eclipse. My main class is in src/programCode/UI_Main2.java
. In that .java
I try to access to a file in src/files/File.file
这是奇怪的事情.
-
如果我使用
/src/files/File.file
,它会给我NoSuchFileException
.
如果我使用src/files/File.file
,它可以在Eclipse中工作,但是当我将其编译为可执行文件.jar时,它会为我提供NoSuchFileException
.
If I use src/files/File.file
it works in Eclipse but when I compile it to a executable .jar it gives me NoSuchFileException
.
如果我使用/files/File.file
,它会给我NoSuchFileException
.
If I use /files/File.file
it gives me NoSuchFileException
.
如果我使用files/File.file
,它会给我NoSuchFileException
.
If I use files/File.file
it gives me NoSuchFileException
.
如果我使用files/File.file
,它会给我NoSuchFileException
.
If I use files/File.file
it gives meNoSuchFileException
.
如果我使用this.getClass().getResource("/files/File.file").getPath().substring(1)
(不带子字符串,它会给我提供无效字符),它会给我NoSuchFileException
(但它显示了绝对路径,并且文件存在于此!)
If I use this.getClass().getResource("/files/File.file").getPath().substring(1)
(without substring it gives me Invalid character) it gives me NoSuchFileException
(but it shows me the absolute path and the file exists there!)
如果我使用this.getClass().getResource("files/File.file").getPath()
,它会给我NullPointerException
并且程序崩溃.
If I use this.getClass().getResource("files/File.file").getPath()
it gives me NullPointerException
and the program crashes.
如果我使用this.getClass().getResource("src/files/File.file").getPath()
,它会给我NullPointerException
并且程序崩溃.
If I use this.getClass().getResource("src/files/File.file").getPath()
it gives me NullPointerException
and the program crashes.
如果我使用this.getClass().getResource("/src/files/File.file").getPath()
,它会给我NullPointerException
并且程序崩溃.
If I use this.getClass().getResource("/src/files/File.file").getPath()
it gives me NullPointerException
and the program crashes.
所以,我不知道该怎么办. src/files/File.file
是唯一可以使用的工具,但是在编译为可执行jar时却不能使用.因此,请帮助我,我尚未找到任何解决方案.谢谢!
So, I don't know what to do. src/files/File.file
is the only one that works, but it doesn't when compiled to executable jar. So please, help me, I haven't found any solution yet.Thanks!
推荐答案
查找文件取决于两件事:
Finding a file depends on two things:
- 您使用绝对路径还是相对路径
- 您的工作目录在哪里
在类似Unix的系统上,当您使用像/dir1/dir2/file
这样的路径时,您使用的是绝对路径,因此您的工作目录并不重要,但是您必须在该路径下有一个文件.
Under Unix-like system when you use path like /dir1/dir2/file
you use absolute path, so your working directory doesn't matter, but you must have a file exactly under that path.
在您的情况下,您尝试使用相对路径,因此不应在一开始就使用/
.
In your case you try to use relative path, so you shouldn't use /
at the beginning.
这种情况对您的问题至关重要:
This case is crucial to your problem:
"If I use src/files/File.file it works in Eclipse but when I compile it to a executable .jar it gives me NoSuchFileException."
默认情况下,Eclipse使用src
的父目录作为工作目录(该目录通常与您的项目无关),因此从那里开始,您的确在该路径下有一个文件.
By default Eclipse uses as working directory a parent directory of src
(which is usually a direcotry with your project", so starting from there you indeed have a file under that path.
启动.jar
时,工作目录位于其他位置.将您的.jar
放在src
的父目录中,它应该可以工作.
When you start a .jar
your working directory is somewhere else. Put your .jar
to parent directory of src
and it should work.
现在,我建议您将文件的位置更改为src
以外的目录(称为Resurces
之类),并将其与.jar
一起提供.
Now, I suggest that you change location of the file to a directory other than src
(call it Resurces
or something) and provide it along with the .jar
.
此外,这是有关工作目录和.jar
文件的有趣讨论:
Also, here is an interesting discussion about working directories and .jar
files:
如果您要分发一个.jar
,请参考以下包装说明:
If you want to distribute a single .jar
here is a good packaging instruction:
http://www.cefns.nau. edu/〜edo/Classes/CS477_WWW/Docs/pack_resources_in_jar.html
这篇关于编译时Java没有在源代码中获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!