在 Eclipse 中调试 Java 时如何使 getResourceAsStream 工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/760756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how do you make getResourceAsStream work while debugging Java in Eclipse?
提问by Jason S
This is mind-boggling... I can make getResource() and getResourceAsStream() work properly when I run Java on my packaged JAR file that includes a text file. (for reference see the Sun docs on accessing resources) I can't seem to make the same program work properly when I am running it within Eclipse, even though I've placed my text file in the same tree as my compiled .class files
这令人难以置信……当我在包含文本文件的打包 JAR 文件上运行 Java 时,我可以使 getResource() 和 getResourceAsStream() 正常工作。(作为参考,请参阅有关访问资源的 Sun 文档)当我在 Eclipse 中运行相同的程序时,我似乎无法使其正常工作,即使我已将文本文件与编译的 .class 文件放在同一棵树中
Can one of you point me at any subtleties to ensure that getResource() and getResourceAsStream() functions work properly?
你们中的一个能指出任何细微之处以确保 getResource() 和 getResourceAsStream() 函数正常工作吗?
I have a hunch it has to do with CLASSPATH and/or where Eclipse puts the .class files it autocompiles. (I've noticed when I run Ant, it compiles all the Java files that have changed since my last Ant build, even though Eclipse has compiled those Java files already.)
我有一种预感,它与 CLASSPATH 和/或 Eclipse 放置它自动编译的 .class 文件的位置有关。(我注意到,当我运行 Ant 时,它会编译自上次 Ant 构建以来更改的所有 Java 文件,即使 Eclipse 已经编译了这些 Java 文件。)
采纳答案by Chochos
If you put your text file along with your source files, Eclipse will copy it to wherever it's placing its compiled .class files, and so you'll see your text file when you run your app from Eclipse (and you will be able to edit the file from within Eclipse as well).
如果您将文本文件与源文件放在一起,Eclipse 会将其复制到放置已编译的 .class 文件的任何位置,因此当您从 Eclipse 运行您的应用程序时,您将看到您的文本文件(并且您将能够编辑Eclipse 中的文件)。
回答by matt b
I have a hunch it has to do with CLASSPATH and/or where Eclipse puts the .class files it autocompiles.
我有一种预感,它与 CLASSPATH 和/或 Eclipse 放置它自动编译的 .class 文件的位置有关。
In the Run Configuration for however you are launching your application, go to the Classpath tab and add the root folder of the path that you are using to access the file.
在 Run Configuration for where you are running your application 中,转到 Classpath 选项卡并添加用于访问文件的路径的根文件夹。
So for example if you are trying to load "blah/file.txt" and the file resides in "folder/blah/file.txt", add "folder" to the classpath. If you are trying to load the resource with just "file.txt", then add "folder/blah".
因此,例如,如果您尝试加载“blah/file.txt”并且文件驻留在“folder/blah/file.txt”中,请将“folder”添加到类路径中。如果您尝试仅使用“file.txt”加载资源,请添加“文件夹/等等”。
回答by Tom Hawtin - tackline
What precisely do you mean by "work properly"? What's the error/behaviour?
您所说的“正常工作”是什么意思?什么是错误/行为?
I suggest using getResource
to find the URL of one of your .class
files (or roughly equivalently getClass().getProtectionDomain().getCodeSource()
. You may find that it is pointing somewhere unexpected.
我建议使用getResource
来查找您的.class
文件之一(或大致等效的getClass().getProtectionDomain().getCodeSource()
.
回答by Scott Stanchfield
A few notes:
一些注意事项:
First -- as Chocos says, put it in the eclipse source dirs, not the binary dirs. Eclipse will clear the binary dirs when you "clean", as well as clean up unmatched files. It will copy non-java source files to the binary dir.
首先——正如 Chocos 所说,把它放在 eclipse 源目录中,而不是二进制目录中。Eclipse 将在您“清理”时清除二进制目录,并清理不匹配的文件。它将非 java 源文件复制到二进制目录。
This means that even though you drop the file in the binary dir, it may be deleted by eclipse...
这意味着即使您将文件放在二进制目录中,它也可能被 eclipse 删除...
Keep in mind that getResourceAsStream and getResource operate relative to the package of the code that calls them. For example:
请记住, getResourceAsStream 和 getResource 相对于调用它们的代码包进行操作。例如:
package a.b.c;
public class Foo {
...
getClass().getClassLoader().getResourceAsStream("fee.txt");
...
}
This will actually look for a/b/c/fee.txt; the package is pre-pended. This works well if you have the fee.txt in the same source dir as the Foo.java file, or if you have a separate set of resource dirs on the classpath with the same directory structure.
这实际上会查找 a/b/c/fee.txt;包是预先挂起的。如果您在与 Foo.java 文件相同的源目录中拥有fee.txt,或者如果您在具有相同目录结构的类路径上有一组单独的资源目录,这将很有效。
If you use
如果你使用
...getResourceAsStream("/fee.txt");
it will look for fee.txt directly on the classpath.
它会直接在类路径上查找fee.txt。
When you run from the command-line, where in the JAR is the resource file? -- Scott
当您从命令行运行时,JAR 中的资源文件在哪里?——斯科特
回答by Jason S
Found the problem!
发现问题了!
I didhave my image files in the source tree. (in my Ant build, I copy them to the build dir)
我确实在源树中有我的图像文件。(在我的 Ant 构建中,我将它们复制到构建目录)
The problem was that I had not done a recent refresh of the source tree in Eclipse. So the files were there but Eclipse wasn't paying any attention to them.
问题是我最近没有在 Eclipse 中刷新源代码树。所以这些文件在那里,但 Eclipse 没有注意到它们。
Thanks for clueing me in to the fact that Eclipse should have them in my source tree.
感谢您让我了解 Eclipse 应该在我的源代码树中包含它们的事实。
回答by BJF
I had the same problem in with IntelliJ, the solution for that was, In project settings, add the folder with the resource files, in my case sound files, as source. Add ?*.wav (or your pref filetype) in Settings->Compiler Resource patterns.
我在 IntelliJ 中遇到了同样的问题,解决方案是,在项目设置中,添加包含资源文件的文件夹,在我的例子中是声音文件,作为源。在 Settings->Compiler Resource patterns 中添加 ?*.wav (或您的偏好文件类型)。