Makky
Makky

Reputation: 17463

opening a file inside the jar

I have an ant script which does lots of stuff but I have been asked to provide jar so it can be run as an app.

I have managed to write the java code to invoke ants programatically but I am having the problem to refrence the build file from the code.

File buildFile = new File(BUILD_FILE);

where BUIlD_File is my build.xml (exists in the main directory).

When I export my proj as Runnable jar it throws an exception (File not found build.xml)

Even if I add the build.xml file into jar still it moans though if I put the build.xml file in the same folder where the jar is then it works fine. (But i dont wanna do this)

Can aynone guide me how can I make sure when I export as jar the build.xml file is added in jar and how can I refrence that file(inside jar) in my code.

Upvotes: 0

Views: 907

Answers (1)

JJ.
JJ.

Reputation: 5475

Any Object running in the JVM can fetch an InputStream for any resource/file within the JAR as follows:

InputStream is = this.getClass().getResourceAsStream("yourpackage/yourfile.xml");

In your case, it sounds like build.xml isn't in a package so you should be able to use:

InputStream is =  this.getClass().getResourceAsStream("build.xml");

Upvotes: 2

Related Questions