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

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

问题描述

我无法读取我从Eclipse项目导出的导出的jar文件中的文件。当我在命令行上运行我的应用程序时,或者在Eclipse中运行相同的应用程序,并且可以通过Eclipse中的源层次结构访问相同的文件(即不通过Jar文件)访问同一个文件时,我想要访问同一个文件。 / p>

在我的Java项目中,我有一个名为SoundDir的文件夹,其中有一个声音文件:sonar-beep.wav。这是我无法访问的文件。



这是我试图用来访问声音文件的代码行:

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

当我在Eclipse Debugger中运行我的程序时,输入会一直返回为Null。我假设没有找到该文件。



任何想法有什么问题?



现在,如果这个做了工作,我想使用inputStream作为Java SoundPlayer的参数来播放此数据文件中的音乐。我可以直接提供输入作为参数,还是需要一个BufferedReader或其他一些对象?

  AudioInputStream audioIn1 = AudioSystem.getAudioInputStream(input); 

感谢任何帮助!



感谢。






好的,现在已经解决了。从罗伯特看下面的解决方案,与图像文件有同样的问题。我希望我的程序可以找到它们,无论应用程序是在Eclipse中还是在命令行中运行。



这里是代码:
ImageIcon imageIcon = new ImageIcon(getImageURL(/ ImageAirplanes / AirplaneRtoL.png));



这里是getImageURL的方法:

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

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

aURL = cl.getResource(imageFilename);

返回aURL;
}

在Eclipse中运行程序时,我继续为aURL获取一个空值。
我确实有ImageAirplanes(这是资源)的父目录,添加到我的项目的Java构建路径。



任何想法?谢谢。

解决方案

您的SoundDir目录位于JAR文件中?尝试拆包JAR文件,并检查您是否不确定。



如果您的SoundDir目录位于JAR目录的内部,那么它与JAR根目录相对,您需要使用 /SoundDir/sonar-beep.wav - 记下领先的斜杠。这使路径绝对相对于根



没有这个斜线,您指定一个相对路径相对于类加载器 - 加载包含在与 getClass()



要更深入地了解,了解文件系统路径


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).

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.

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

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

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?

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);

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

Thanks.


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.

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

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;
}

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.

Any ideas? Thanks.

解决方案

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

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

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天全站免登陆