Java: Questions about reading resources from the Jar GetClass (). GetResource (...)

Source: Internet
Author: User
Tags file url

In the Java program release, many people will choose to use binary jar format for publishing, how to read the resources inside the jar?
The main use of ClassLoader the following several methods to achieve:
Public URL getresource (String name);
Public InputStream getResourceAsStream (String name)

public static InputStream Getsystemresourceasstream (String name)
public static URL Getsystemresource (String name)

I usually use:

GetClass (). GetResource (...);

For example, set the icon for JFrame:

This.seticonimage (New ImageIcon (GetClass (). GetResource (...)). GetImage ());

But the problem I'm having today is that in the program you need to play a notify.wav sound file stored in the jar, My Code:

System.out.println (GetClass (). GetResource ("/sound/notify.wav"). GetPath ());
System.out.println (GetClass (). GetResource ("/sound/notify.wav"). GetFile ());
System.out.println (GetClass (). GetResource ("/sound/notify.wav"). Toexternalform ());
System.out.println (GetClass (). GetResource ("/sound/notify.wav"). toString ());

/*

Path to Output

/d:/soft/test/bin/sound/notify.wav
/d:/soft/test/bin/sound/notify.wav
File:/d:/soft/test/bin/sound/notify.wav
File:/d:/soft/test/bin/sound/notify.wav

*/

FileInputStream Audiofis = new FileInputStream (GetClass (). GetResource ("/sound/notify.wav"). GetFile ());

AudioPlayer.player.start (Audiofis); Play sound

When tested in eclipse, the sound is played normally, but when packaged into a jar file, it is not possible to find a sound file.

Why is the icon in the Top Settings window possible, but there is an error playing the sound file in the jar here?

Finally through the online search, find this introduction (http://www.iteye.com/topic/483115) only understand:

Excerpt part of the content: ........ ..........

This is mainly because the jar package is a separate file rather than a folder, and it is absolutely impossible to locate res.txt through the "file:/e:/.../resourcejar.jar/resource/res.txt" file URL. So even with a relative path, you can't locate the TXT file within the jar file (the reader may have some confusing explanations for this reason, and we'll elaborate on the results of a run of code below).

Because ".../resourcejar.jar!/resource/..." is not the format of the file resource Locator (the resource in the jar has its own special URL form: Jar:<url>!/{entry}). Therefore, if the class source code in the jar package is in the form of file F=new files (relative path), it is not possible to locate the document resource. This is why when the source code 1 is packaged into a jar file, it is reported that the FileNotFoundException is the crux of the call to the jar package.

The solution is also simple, direct use:getclass (). getResourceAsStream ()

InputStream Audiois = GetClass (). getResourceAsStream ("/sound/notify.wav");;
AudioPlayer.player.start (Audiois);

2012-01-29

Java: Questions about reading resources from the Jar GetClass (). GetResource (...)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.