需要帮助的访问数据文件中的JAR文件 [英] Need help accessing data file in Jar File

查看:115
本文介绍了需要帮助的访问数据文件中的JAR文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦读取一个文件,是在我从我的Eclipse项目导出导出jar文件。我想acesss此相同的文件在命令行中运行我的应用程序或者当,或运行时,在Eclipse相同的应用程序,并将它通过源层次从Eclipse中访问同一个文件(也就是不通过的JAR文件)。

I am having trouble reading a file that is in an exported jar file I had exported from my Eclipse project. I would like to acesss this same file when either running my application on the command line, or when running the same application within Eclipse and have it access the same file via the source hierarchy from within Eclipse (i.e not via the Jar File).

在我的Java项目中,我有一个文件夹,名为:SoundDir,并在它被称为声音文件:声纳beep.wav。这是我无法访问文件。

In my Java project, I have a folder called: SoundDir, and within it a sound file called: sonar-beep.wav . This is the file I am having trouble accessing.

下面是code线我试图用来访问声音文件:

Here is the line of code I am trying to use to access the sound file:

InputStream input = getClass().getResourceAsStream("SoundDir/sonar-beep.wav");

当我在Eclipse中调试运行我的程序,输入不断回来为NULL。我认为它没有找到想要的文件。

When I run my program in the Eclipse Debugger, input keeps coming back as Null. I assume it is not finding the file.

这有什么不对任何想法?

Any ideas on what is wrong?

现在,如果没有工作,我会想使用的InputStream作为参数传递给了Java的SoundPlayer该数据文件播放音乐。我能提供的输入直接作为参数,还是我需要一个BufferedReader或在它上面的其他对象?

Now, if this did work, I would want to use the inputStream as an argument to the Java SoundPlayer to play the music in this data file. Can I supply "input" directly as the parameter, or do I need a BufferedReader or some other object on top of it?

AudioInputStream audioIn1= AudioSystem.getAudioInputStream(input);

请问AP preciate任何帮助!新的所有这些类加载器的问题。

Would appreciate any help on this! New to all these classLoader issues.

感谢。

好了,现在,这是解决了。看到下面的罗伯特解决方案,具有图像文件相同的问题。我想我的程序能够找到它们,应用程序是否在Eclipse中运行,或者在命令行上。

Ok, now that this is solved. See solution below from Robert, having same issue with image files. I want my program to be able to locate them, whether the application is running within Eclipse, or on the command line.

下面是code:
的ImageIcon ImageIcon的=新的ImageIcon(getImageURL(/ ImageAirplanes / AirplaneRtoL.png));

Here is the code: ImageIcon imageIcon= new ImageIcon(getImageURL("/ImageAirplanes/AirplaneRtoL.png"));

这是方法getImageURL:

And here is the method getImageURL:

public URL getImageURL(String imageFilename) {
    URL aURL= null;

    ClassLoader cl = this.getClass().getClassLoader();

    aURL= cl.getResource(imageFilename);

     return aURL;
}

我继续运行在Eclipse程序时得到一个空值aURL。
而且我有ImageAirplanes的父目录(即资源),添加到Java构建路径为我的项目。

I keep getting a null value for aURL when running the program within Eclipse. And I do have the parent directory of ImageAirplanes (which is "Resources"), added to the Java Build Path for my project.

任何想法?谢谢你。

推荐答案

其中位于JAR文件的目录SoundDir?尝试拆包JAR文件和检查,如果你不知道。

Where is your SoundDir directory located in the JAR file? Try unpacking the JAR file and checking if you are not sure.

如果您SoundDir目录位于立即里面JAR'd目录,那么它是相对于JAR根目录,你需要使用 /SoundDir/sonar-beep.wav - 注意斜线。这使得路径的绝对相对根

If your SoundDir directory is located in the immediately inside the JAR'd directory, then it is relative to the JAR root directory, and you need to use /SoundDir/sonar-beep.wav - note the leading slash. This makes the path absolute, or relative to root

如果没有那条斜线,你指定的相对路径,即相对于类加载器 - 加载包含在同一个包中的类通过返回的资源的getClass()

Without that slash, you are specifying a relative path, which is relative to the classloader - loading a resource that is contained in the same package as the class returned by getClass().

要更深刻地理解,它有助于了解文件系统路径

To understand more deeply, it is helpful to understand File System paths

这篇关于需要帮助的访问数据文件中的JAR文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆