My 2 cents.
1) / is root of jar file. So, if it's in dir "dir" in jar it will be
OK.
2) I dont't know why but you can't read files as resources from root
directly.
They must be in some directory.
So, create jar with at least one directory "dir", place file here and
read it as
/dir/a.txt
It works.
import java.io.*;
public class A
{
public static void main( String[] args)throws Exception
{
A app = new A();
BufferedReader bf = new BufferedReader(
new InputStreamReader(app.getClass().getResourceAsStream("txt/b.txt"))
);
if( bf.ready())System.out.println(bf.readLine());
}
}
Sorry It's still failure
E:\code\java\test\jar\test>java -jar a.jar
Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Unknown Source)
at java.io.InputStreamReader.<init>(Unknown Source)
at A.main(A.java:7)
"Alex" <akizub@yahoo.com>
??????:1134169849.622062.308530@g47g2000cwa.googlegroups.com...
> My 2 cents.
> 1) / is root of jar file. So, if it's in dir "dir" in jar it will be
[quoted text clipped - 8 lines]
>
> It works.
Roedy Green - 10 Dec 2005 02:44 GMT
>E:\code\java\test\jar\test>java -jar a.jar
>Exception in thread "main" java.lang.NullPointerException
> at java.io.Reader.<init>(Unknown Source)
> at java.io.InputStreamReader.<init>(Unknown Source)
> at A.main(A.java:7)
lets see a directory of your jar to make sure you have everything
named correctly.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Alex - 10 Dec 2005 02:51 GMT
You didn't include / !!!
It means that directory txt msut be where you class is.
For example if it is class abc.xyz.Test
then directory and file should be (in the jar) /abc/xyz/txt/b.txt
So I suggest always use root and have it like
/config/b.txt
pvii007 - 10 Dec 2005 04:40 GMT
That's OK.
Thank you for your help.
Another problem.
If I have a file structure
/core/ (here is application)
/core/doc ( here is the doc )
/core/doc/filecollection1/
/core/doc/filecollection1/subcollection1/
/core/doc/filecollection1/subcollection2/
/core/doc/filecollection2/
/core/doc/filecollection2/subcollection1/
/core/doc/filecollection2/subcollection2/
I compress the file and doc in the jar file.I want to access to every dir or
file recursively.
How to write my code?
PS:
I have written the code
...
File codebase = new File("/core/doc");
...
public String read_file_recursive( File file)
{
String ret;
if( codebase.isDirectory())
{
File[] filelist = codebase.listFiles();
for( int i = 0 ; i < filelist;i++ )
{
ret += read_file_recursive(filelist[i]);
}
}
else
{
// read file content ...
}
return ret;
}
...
But it doesn't work under the jar file.
"pvii007" <pvii007@yahoo.com.cn> дÈëÏûÏ¢ÐÂÎÅ:dndef5$r8g$1@news.yaako.com...
> import java.io.*;
> public class A
[quoted text clipped - 31 lines]
>>
>> It works.
Roedy Green - 10 Dec 2005 07:36 GMT
>But it doesn't work under the jar file.
If you wanted it to look inside jar files, you would need to use a
filter to find the *.jar files, and individually read them with the
jar classes. Members are not part of the file system.
See http://mindprod.com/jgloss/jar.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.