Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Using files in META-INF  (Read 196 times)
0 Members and 1 Guest are viewing this topic.
Offline Anju_Maaka

Senior Newbie


Exp: 2 years



« Posted 2014-04-24 13:55:26 »

I can put a file into the META-INF folder easily (just change the type to ZIP and then move it there) but how should I do to load the file in the program?
I'm fairly inexperienced with loading files and don't even know how to load something from a folder lying at the same place as the JAR file, is there a difference to this compared to loading something from the META-INF?
If possible I'd like to be able to put folders in the META-INF folder and then load files from those folders (it'd make file handling a bit easier for me)
Offline junkdog
« Reply #1 - Posted 2014-04-24 14:05:03 »

Class#getResource is what you're looking for, see: http://stackoverflow.com/a/1273432/431535

artemis-odb: bugfixing and performance optimized fork of artemis ES
Offline Anju_Maaka

Senior Newbie


Exp: 2 years



« Reply #2 - Posted 2014-04-24 15:01:18 »

@junkdog
I'm sorry. First of all I don't quite understand that code you linked to and also I'm not interested in the Manifest file. I want to get, say, an image "ball.png" which lies in the META-INF map (or anywhere else in the JAR file)
I use the following code to load Native files from a folder which lies parallel to the JAR file (they are in the same folder). Let's say I want to move that "nat"-folder into the JAR file so that I only have to keep track of one file

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
public static void load() {
         
         String system = System.getProperty("os.name");
         
         String path = null;
         path = new File("nat").getAbsolutePath();         //This here is where the path into
                                                          //the folder is set right?

         if(path == null) {
            System.out.println("Couldn't load natives!");
            return;
         }

         path += "\\";
         
         if (system.contains("Windows")) {
           
             System.setProperty("org.lwjgl.librarypath", path + "Windows");
         }
         else if (system.contains("Mac")) {
             System.setProperty("org.lwjgl.librarypath", path + "Mac");
         }
         else if (system.contains("Linux")) {
             System.setProperty("org.lwjgl.librarypath", path + "Linux");
         }
         
      }
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Grunnt

JGO Wizard


Medals: 64
Projects: 8
Exp: 5 years


Complex != complicated


« Reply #3 - Posted 2014-04-24 15:28:59 »

A jar file is an archive that you need to treat differently from the normal file system. You acccess resources inside a jar through a classloader. If you put the image in /nat inside the jar file I think this should work to load it:

1  
2  
3  
ClassLoader loader = this.getClass().getClassLoader();
java.net.URL imageURL   = loader.getResource("/nat/image.png");
ImageIcon image = new ImageIcon(imageURL);

Offline Anju_Maaka

Senior Newbie


Exp: 2 years



« Reply #4 - Posted 2014-04-24 16:08:02 »

@Grunnt
I tried it. Afraid it didn't work   Sad
This has turned out to be a lot harder than I thought it would be
Offline Anju_Maaka

Senior Newbie


Exp: 2 years



« Reply #5 - Posted 2014-04-24 17:01:01 »

Ok, I got it working.
I can load an image like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public BufferedImage loadImage(String path){
      java.net.URL imgURL = getClass().getResource(path);
      if(imgURL != null){
         try {
            return ImageIO.read(imgURL);
         } catch (IOException e) {
            e.printStackTrace();
            return null;
         }
      }
      else{
         System.err.println("Couldn't find file: " + path);
           return null;
      }
   }
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (11 views)
2014-07-24 01:59:36

Riven (9 views)
2014-07-23 21:16:32

Riven (11 views)
2014-07-23 21:07:15

Riven (12 views)
2014-07-23 20:56:16

ctomni231 (42 views)
2014-07-18 06:55:21

Zero Volt (38 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (34 views)
2014-07-16 23:30:00

Cero (49 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!