Reading a file outside a jar from inside the jar

Hi,

I have the following:

C:\Program\bin\myJar.jar

C:\Program\config\myConfig.xml

There is a class inside the jar that wants to get an InputStream on myConfig.xml (and later write back to it). The class can only know the relative path to myConfig.xml i.e. ..\config\myConfig.xml

Anyone know how to do this?

I have tried all sorts with no luck, currently:

getClass().getResourceAsStream("..\\config\\myConfig.xml");

Been on it for about two hours now.

Any Ideas?

Cheers,

Jim

[546 byte] By [patumairea] at [2007-11-27 4:44:00]
# 1

import java.security.*;

import java.net.*;

import javax.swing.*;

public class CodeSourceExample {

public static void main(String[] args) {

Class cls = CodeSourceExample.class;

ProtectionDomain domain = cls.getProtectionDomain();

CodeSource source = domain.getCodeSource();

URL url = source.getLocation();

JOptionPane.showMessageDialog(null, url.toString());

}

}

If run from a jarred application, that url is the location of the jar, for example "file:/path.../app.jar". You could parse it to find the path of the folder containing the jar and take it from there...

BTW, if the app isn't jarred, the URL is "file:/path...", giving the folder that holds CodeSourceExample.class

Hippolytea at 2007-7-12 9:55:52 > top of Java-index,Java Essentials,Java Programming...
# 2

> Hi,

> I have the following:

>

> C:\Program\bin\myJar.jar

> C:\Program\config\myConfig.xml

>

> There is a class inside the jar that wants to get an

> InputStream on myConfig.xml (and later write back to

> it). The class can only know the relative path to

> myConfig.xml i.e. ..\config\myConfig.xml

This works just fine

package somepackage;

import java.io.*;

public class Main {

public static void main(String[] args) throws Exception {

File file = new File("../resources/file.txt");

BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));

writer.write("something");

writer.close();

}

}

C:\SomeDir\bin\TheJar.jar

C:\SomeDir\resources\file.txt

Manifest

Manifest-Version: 1.0

Ant-Version: Apache Ant 1.6.5

Main-Class: somepackage.Main

Message was edited by:

shittybytes

shittybytesa at 2007-7-12 9:55:52 > top of Java-index,Java Essentials,Java Programming...
# 3

Thanks a lot guys!

Shittybytes:

I cant make that work as you said; are you sure this works when the code is jarred?

Hippolyte:

Your code does what you say but I still cant get it to find my config file.

I have constructed the path to the config file by hacking the path I get from your code and then I do getResourceAsStream on that path.

but I get IOException stream closed

Have you got any more ideas?

Thanks a lot,

Jim

patumairea at 2007-7-12 9:55:52 > top of Java-index,Java Essentials,Java Programming...
# 4

>I have constructed the path to the config file by hacking the path I get from your code and then I do getResourceAsStream on that path.

Be sure to hack up a path, not a url string (no "file:" protocol, right?) and use

that string to construct a java.io.File and go from there, for example. Give

up on getResourceAsStream as that is not going to work it's way from one jar

to another.

Hippolytea at 2007-7-12 9:55:52 > top of Java-index,Java Essentials,Java Programming...