Eclipse's experience exporting jar files with pictures, sound effects, and other binaries

Source: Internet
Author: User

Let's start with the simple process:

1, select "Project", right-"export-" java "jar file"-"Export generated clas files and resources"-"next-" next-"in" Select the class of The application entry point "Inside Select Entry Class-" finish.

2, the process does not look complicated, but for a person who just learned Java, may be crazy: Java is case-sensitive, including the file name. This must be noted!

3, before the export, it is best to right-click on the refresh, otherwise if you add a file or delete files or other renaming of files, etc., make you inexplicable.

Next I met the problem and how to solve it, I beginner Java, only know can solve, do not know the standard answer, do not know there is no better answer.

1, I have 3 resource folders, respectively, is images, musics, map, which are placed in pictures (jpg, PNG, etc.), sound (MIDI), binary files. Before you start packing, these folders are on the outermost side, which is the same as the SRC directory. After debugging, I ran export package. After executing the packaged JAR file, the resource file cannot be found! I got a half-day, did not fix, see the help of the Internet, know to use Xx.class.getResource (/images/xx.jpg) This method, but not ready to write test code analysis, the original generated jar file root directory is not src parent directory, Instead, compile the generated bin folder!

Solution: Put images, musics, map directory in the SRC directory, right-click the project refresh, will automatically sync to the bin directory (this is a bit magical).

2, the project inside All Toolkit.getdefaulttoolkit (). GetImage (strfile) Open the file in the same way as New ImageIcon (Xx.class.getResource (str)). GetImage () This format, such as the original image IMGBG = Toolkit.getdefaulttoolkit (). GetImage ("Images\\background.png");

Replace with imgbg= new ImageIcon (Test.class.getResource ("/images/background.png"). GetImage (); Here are two points to note: The first is images in front to add "/"; the second is not to use Test.class.getResource ("/images/background.png"). GetFile ()! You must open the URL with ImageIcon and then get the image from ImageIcon.

3, will be in the project all file File = new file (fileName) read files; Replace with InputStream is = FileTool.class.getResourceAsStream ("/... Here is the file path filename "); This way.

To put it bluntly, the file inside the jar operation can only use the relative path of the root directory, and the relative path of the root directory is located by Class! In addition, the jar open the file inside, to use getresource get the URL or getresourceasstream get InputStream!

Give some code examples:

imgbg= New ImageIcon (Test.class.getResource ("/images/background.png")). GetImage ();

InputStream in = FileTool.class.getResourceAsStream ("/map/grade" + index + ". Dat");

DataInputStream din = new DataInputStream (in);

sequence = Midisystem.getsequence (GameMidi.class.getResource (/musics/1.mid));

Eclipse's experience exporting jar files with pictures, sound effects, and other binaries

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.