Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (540)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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 271 times)
0 Members and 1 Guest are viewing this topic.
Offline Anju_Maaka

Senior Newbie


Exp: 2 years



« Posted 2014-04-24 11: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 12: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 ECS
Offline Anju_Maaka

Senior Newbie


Exp: 2 years



« Reply #2 - Posted 2014-04-24 13: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 Kernel


Medals: 95
Projects: 8
Exp: 5 years


Complex != complicated


« Reply #3 - Posted 2014-04-24 13: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 14: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 15: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  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

Mr.CodeIt (20 views)
2014-12-23 03:34:11

rwatson462 (53 views)
2014-12-15 09:26:44

Mr.CodeIt (45 views)
2014-12-14 19:50:38

BurntPizza (83 views)
2014-12-09 22:41:13

BurntPizza (110 views)
2014-12-08 04:46:31

JscottyBieshaar (78 views)
2014-12-05 12:39:02

SHC (89 views)
2014-12-03 16:27:13

CopyableCougar4 (93 views)
2014-11-29 21:32:03

toopeicgaming1999 (155 views)
2014-11-26 15:22:04

toopeicgaming1999 (151 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!