简体   繁体   English

在Eclipse中调试Java时,如何使getResourceAsStream工作?

[英]how do you make getResourceAsStream work while debugging Java in Eclipse?

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. 这令人难以置信...当我在包含文本文件的打包JAR文件上运行Java时,我可以使getResource()和getResourceAsStream()正常工作。 (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 (有关参考,请参见有关访问资源的Sun文档)。即使我将文本文件与已编译的.class文件放在同一棵树中,当在Eclipse中运行该程序时,我似乎也无法使其正常运行。

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. 我有一个预感,它与CLASSPATH和/或Eclipse将其自动编译的.class文件放在何处有关。 (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.) (我已经注意到,当我运行Ant时,即使Eclipse已经编译了这些Java文件,它也会编译自上次Ant构建以来所有已更改的Java文件。)

A few notes: 一些注意事项:

First -- as Chocos says, put it in the eclipse source dirs, not the binary dirs. 首先-正如Chocos所说,将其放入日食源目录中,而不是二进制目录中。 Eclipse will clear the binary dirs when you "clean", as well as clean up unmatched files. 当您“清理”时,Eclipse将清除二进制目录,以及清理不匹配的文件。 It will copy non-java source files to the binary dir. 它将非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. 请记住,getResourceAsStream和getResource相对于调用它们的代码包进行操作。 For example: 例如:

package a.b.c;
public class Foo {
   ...
   getClass().getClassLoader().getResourceAsStream("fee.txt");
   ...
}

This will actually look for a/b/c/fee.txt; 这实际上将寻找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. 如果您在与Foo.java文件相同的源目录中具有fee.txt,或者如果您在类路径上具有相同目录结构的一组单独的资源目录,则此方法就很好用。

If you use 如果您使用

...getResourceAsStream("/fee.txt");

it will look for fee.txt directly on the classpath. 它将直接在classpath上查找fee.txt。

When you run from the command-line, where in the JAR is the resource file? 从命令行运行时,资源文件在JAR中的哪个位置? -- Scott -斯科特

如果将文本文件和源文件一起放置,Eclipse会将其复制到放置其编译后的.class文件的任何位置,因此从Eclipse运行应用程序时,您将看到文本文件(并且您将能够进行编辑Eclipse中的文件)。

Found the problem! 发现了问题!

I did have 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. 问题是我没有在Eclipse中刷新源树。 So the files were there but Eclipse wasn't paying any attention to them. 因此文件已经存在,但是Eclipse并未对其进行任何关注。

Thanks for clueing me in to the fact that Eclipse should have them in my source tree. 感谢您让我了解Eclipse应该在我的源代码树中包含它们的事实。

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. 我在IntelliJ中也遇到了同样的问题,解决方案是,在项目设置中,将包含资源文件(在我的情况下为声音文件)的文件夹添加为源。 Add ?*.wav (or your pref filetype) in Settings->Compiler Resource patterns. 在“设置”->“编译器资源”模式中添加?*。wav(或您的首选项文件类型)。

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. 但是,在要启动应用程序的“运行配置”中,转到“类路径”选项卡,然后添加用于访问文件的路径的根文件夹。

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. 因此,例如,如果您尝试加载“ blah / file.txt”并且文件位于“ folder / blah / file.txt”中,则将“ folder”添加到类路径中。 If you are trying to load the resource with just "file.txt", then add "folder/blah". 如果您尝试仅使用“ file.txt”加载资源,则添加“ folder / blah”。

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文件的URL(或大致等效的getClass().getProtectionDomain().getCodeSource() ,您可能会发现它指向了意外的地方。

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

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