Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading an xml file inside a Jar

 
Hesham Gneady
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Jamir Shaikh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That question is more difficult than we usually see here on beginners'. Moving.
 
Lucas Franceschi
Ranch Hand
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
Hesham Gneady
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?



 
Lucas Franceschi
Ranch Hand
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marky Vasconcellos wrote:


actually, after you have your input stream, you can do anything, that includes loading files objects.
 
Lucas Franceschi
Ranch Hand
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
welcome to javaranch Marky Vasconcellos!
 
Marky Vasconcellos
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21298
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Posts: 21298
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Rob for the help ... and thanks other fellows who replied.
Now i understand it.
 
kowshik ns
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will it work when deplyed in jboss server
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic