Solved

JAVA storeing files in META-INF directory

Posted on 2004-03-25
7
515 Views
Last Modified: 2008-03-17
I stored an xml file in the JARs META-INF directory and want to read that file using a path relative to the JARs root

this is what I'm doing:
ClassLoader cl = Thread.currentThread().getContextClassLoader();
URL url = cl.getResource( "META-INF/test.xml" );
Initializer init = new Initializer(new File(url.getFile()).getAbsolutePath());

The absolute path is returned as: C:/Jar_root/abc.jar!/META-INF/test.xml

If I try to use this as a file path I get a FileNotFoundException due to a illegal path syntax.

I need to use it this because inside the jar i have an entry point function, and I want to be able to run this:

C:\Jar_root\java -jar abc.jar

inside this jar, the first thing it'll do is it needs to go to the xml file stored inside the META-INF directory to get some information.

and I can't figure why it's complaining about the FileNotFound.  it's trying to get into the jar file, but it can't.

any help is much appreciated! thanks
0
Comment
Question by:energie
  • 3
  • 2
  • 2
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10683344
BufferedReader in = new BufferedReader(new InputStreamReader(cl.getResourceAsStream( "/META-INF/test.xml" )));

should do it
0
 

Author Comment

by:energie
ID: 10683864
Do you see where that problem comes from?  i've tried what you suggested and it doesn't work.  I dont think i made a syntatic error, I might be missing something somewhere

thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 10684068
try moving your xml file outside META-INF.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:energie
ID: 10685025
Hi 'objects',

unfortunatley, if i move the xml files outside the  META-INF directory, i will not be able to even locate it using the class-loader, since that's one of the places it looks for files to load.

dang it, i know I'm so close to the answer.

if i look at this:
C:/Jar_root/abc.jar!/META-INF/test.xml

i can see that it is TRYING to get to the META-INF directory inside this Jar file, if i manually unpack the jar file, i can confirm that there is a META-INF directory as expected.

I don't know what I am missing. :(
0
 
LVL 92

Expert Comment

by:objects
ID: 10685653
>  i will not be able to even locate it using the class-loader

Why do you think that? Try it and see :)

The meta-inf directory is not the place for data files.

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 100 total points
ID: 10685768
The standard approach to this works fine, and although i agree your own data files shouldn't go into META-INF the following works fine after adding the appropriate file to the archive:


import java.io.IOException;
import java.io.InputStream;

public class ReadFromMetaInf {

  public static void main(String[] args) {
    ReadFromMetaInf x = new ReadFromMetaInf();
            try {
                  x.displayContent();
            }
            catch(IOException e) {
                  e.printStackTrace();
            }
  }
      
      public void displayContent() throws IOException {
            InputStream in = getClass().getResourceAsStream("/META-INF/x.txt");
            int buf = -1;
            while ((buf = in.read()) > -1) {
                  System.out.write(buf);
            }
            in.close();
      }
}


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10690419
8-)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
thymeleaf natural templating vs JSP 2 95
netstat -ano | find "8000" and taskkill /f /pid 2984 3 56
reverse digits of a number using for loop 5 40
spring jars download 1 26
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question