This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Reading an xml file inside a Jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reading an xml file inside a Jar" Watch "Reading an xml file inside a Jar" New topic
Author

Reading an xml file inside a Jar

Hesham Gneady
Ranch Hand

Joined: Feb 26, 2007
Posts: 66
Hello ,

I have problems understanding the Manifest file & Class-Path regarding creating a jar file for my desktop application.
For example :
I created a java application with the following main class :


The application just tries to access & read an xml file in the path "db/test.xml" inside the application.
Now when creating a jar for this app. & running the main class, i get the following Exception :

C:\>java -jar app.jar
java.io.FileNotFoundException: C:\db\test.xml (The system cannot find the path specified)


Do i need to set something in the Manifest file to access the xml file ?

Thanks in advance,
Hesham


Hesham
Jamir Shaikh
Greenhorn

Joined: Aug 13, 2008
Posts: 7
It is searching default for C:
You either hard code the file path or
you can pass a command line parameter to your main.
Hesham Gneady
Ranch Hand

Joined: Feb 26, 2007
Posts: 66
Yes ... That part that i want to understand !!
I want to know why it's not searching inside the Jar & searches the current path i'm in "c:/" instead ?

About inserting an file path hard-coded or as a cmd line parameter, i can't do that because i want to deliver the jar to a client so i must make things easy for him.
So is there any other solution ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38041
    
  22
That question is more difficult than we usually see here on beginners'. Moving.
Lucas Franceschi
Ranch Hand

Joined: Nov 10, 2008
Posts: 106

You will need to use a input stream.

the thing is that if your class was able to be loaded from the jar, then you ought to be able to ask this same classloader to load something else,

try using this:


just putting the Exactly title of your thread ("Reading an xml file inside a Jar") in google, you will find this, and probably this too.


so, read the links and make some test, it should work (i'm not sure).

regards!

[UPD: in the google search, the two first results is the ones that helped me]


Lucas Franceschi
Software Developer for SGI Sistemas, lukas1596@gmail.com
Hesham Gneady
Ranch Hand

Joined: Feb 26, 2007
Posts: 66
Thanks for the reply ... I'm sure this will work, but this is not logical at all !!
If i want to create a Jar for my application then i can't use "File" class to be able to read files ?

There must be a way to read a File path using the "File" class if the file is inside the Jar too.

I thought this would happen using the Manifest file to specify the internal paths of files/folders/jars inside the application Jar.
Am i wrong about that ?
Lucas Franceschi
Ranch Hand

Joined: Nov 10, 2008
Posts: 106

well, I dont understand a lot about java classloader at all...

but is seems like only java classloader can read jar files, so that you ought to use a classloader to be able to see inside a jar.

so that if you use a file object, you must specify a object by giving the path to it, the real path, so that you'll not be able to make your file object load from inside a jar.

but when you use java classloader to load your file and generate your inputstring, it works because classloader can see inside jars.



i'm not sure, i dont know it very well..

hope i've helped.

Marky Vasconcellos
Ranch Hand

Joined: Jan 28, 2009
Posts: 36
Hesham Gneady wrote:Thanks for the reply ... I'm sure this will work, but this is not logical at all !!
If i want to create a Jar for my application then i can't use "File" class to be able to read files ?

There must be a way to read a File path using the "File" class if the file is inside the Jar too.

I thought this would happen using the Manifest file to specify the internal paths of files/folders/jars inside the application Jar.
Am i wrong about that ?





Each of their nuggets of wisdom contracted to a sound bite: Joshua Bloch: Write Lots of Code; Chet Haase: Don't Put Your Entire Application in One Method; Masood Mortazavi: Start Simple and Keep Learning; Cay Horstmann: First, Don't Panic
Lucas Franceschi
Ranch Hand

Joined: Nov 10, 2008
Posts: 106

Marky Vasconcellos wrote:


actually, after you have your input stream, you can do anything, that includes loading files objects.
Lucas Franceschi
Ranch Hand

Joined: Nov 10, 2008
Posts: 106

welcome to javaranch Marky Vasconcellos!
Marky Vasconcellos
Ranch Hand

Joined: Jan 28, 2009
Posts: 36
Lucas Franceschi wrote:welcome to javaranch Marky Vasconcellos!


ty

now i feel home..



so.. he ask if he can load a File inside the jar.. i think it was cause he has a method who need a File as parameter.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Hesham Gneady wrote:Thanks for the reply ... I'm sure this will work, but this is not logical at all !!
If i want to create a Jar for my application then i can't use "File" class to be able to read files ?

There must be a way to read a File path using the "File" class if the file is inside the Jar too.
Am i wrong about that ?
It's perfectly logical. A File is anything that the operating system considers to be a file. The operating system doesn't look inside your jar file and it doesn't consider anything inside it to be a file. It's better to think of things inside your jar file to be "resources". That way you won't be wrong about it.
Lucas Franceschi
Ranch Hand

Joined: Nov 10, 2008
Posts: 106

Marky Vasconcellos wrote:
ty

now i feel home..

good to know, feel like you were home! just don't run naked.

Marky Vasconcellos wrote:
so.. he ask if he can load a File inside the jar.. i think it was cause he has a method who need a File as parameter.


yes you got it.
Lucas Franceschi
Ranch Hand

Joined: Nov 10, 2008
Posts: 106

Paul Clapham wrote:It's perfectly logical. A File is anything that the operating system considers to be a file. The operating system doesn't look inside your jar file and it doesn't consider anything inside it to be a file. It's better to think of things inside your jar file to be "resources". That way you won't be wrong about it.


so paul, a jar file isn't just the same structure as any other compressed files? so OS dont recognize compressed files (classes in this case) as a file?
Marky Vasconcellos
Ranch Hand

Joined: Jan 28, 2009
Posts: 36
Lucas Franceschi wrote:
Marky Vasconcellos wrote:
ty

now i feel home..

good to know, feel like you were home! just don't run naked.


Don't me let drink so much and we can be friends xD
Lucas Franceschi
Ranch Hand

Joined: Nov 10, 2008
Posts: 106

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Lucas Franceschi wrote:

If you use the class loader to load a resource, the resource name should be absolute, and start with a /. If you want to load a resource relative to your class file, you can just skip the class loader and let the class load the resource for you:

The good thing about using Class to load is, you can use both relative and absolute resource names.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Lucas Franceschi wrote:so paul, a jar file isn't just the same structure as any other compressed files? so OS dont recognize compressed files (classes in this case) as a file?


Well, you could try that for yourself. Just go to the command line, change the current directory to something containing a jar file, and see what "dir" or "ls" does with that jar file.
Lucas Franceschi
Ranch Hand

Joined: Nov 10, 2008
Posts: 106

Paul Clapham wrote:
Well, you could try that for yourself. Just go to the command line, change the current directory to something containing a jar file, and see what "dir" or "ls" does with that jar file.


thanks, I thought it was going to be something like this, but had no sure about it.
Hesham Gneady
Ranch Hand

Joined: Feb 26, 2007
Posts: 66
Thanks a lot Rob for the help ... and thanks other fellows who replied.
Now i understand it.
kowshik ns
Greenhorn

Joined: Sep 25, 2009
Posts: 1
Will it work when deplyed in jboss server
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

kowshik ns wrote:Will it work when deplyed in jboss server

Have you tried it?

You may want to read these FAQ entries:
http://faq.javaranch.com/java/ShowSomeEffort
http://faq.javaranch.com/java/DontWakeTheZombies
 
Consider Paul's rocket mass heater.
 
subject: Reading an xml file inside a Jar
 
Similar Threads
Java2WSDL in AXIS 1.4 shows shows java.lang.NoClassDefFoundError: org/apache/axis/wsdlgen/Java2WSDL
JAR file problem
Submission Jar: test case + Ant build script
How to convert XML into HTML?
URLyBird: runme.jar