Reading From a JAR

I understand that when you import a file that is inside a jar, you have to use an import stream. But this program that I'm making is not importing a file but rather going through directories to see if they exist. This code works fine in the compiler but when put into a jar it crashes.

Exception in thread"main" java.lang.NullPointerException

at Main.UserWindow.createUserList(UserWindow.java:118)

So how can I check directories in java without using the File object? Btw, things that might be unclear in the following code are:

classpath= \ or / ,depending if ur on a mac or pc.

userList is a JList.

publicvoid createUserList(){

char firstpath;

char secondpath;

String userspath="users"+classPath;

String[] names =null;

LinkedList<String> nameslst=new LinkedList<String>();

for(int i=0;i<26;i++){

firstpath=(char) (i+'a');

for(int j=0;j<26;j++){

secondpath=(char) (j+'a');

File file=new File(userspath+firstpath+classPath+secondpath);

File[] files=file.listFiles();

for(int k=0;k<files.length;k++){

String filename=files[k].getPath().substring(files[k].getPath().lastIndexOf(classPath)+1, files[k].getPath().length());

if(filename.charAt(0)!='.')

nameslst.add(filename);

}//end for with k

}//end for with j

}//end for with i

names=new String[nameslst.size()];

for(int i=0;i<nameslst.size(); i++){

names[i]=nameslst.get(i);

}

userList.setListData(names);

}

>

[2697 byte] By [blackmagea] at [2007-11-27 9:21:10]
# 1

String filename=files[k].getPath().substring(files[k].getPath().lastIndexOf(classPath)+1, files[k].getPath().length());

That looks doomed for failure. Why don't you store the String that getPath() returns in a variable, seeing as how you use it several times... my bet is that you're NPE is coming from here ... but your compiler is already telling you. Could you please identify which line is 118?

Navy_Codera at 2007-7-12 22:14:50 > top of Java-index,Java Essentials,Java Programming...
# 2

> > String

> filename=files[k].getPath().substring(files[k].getPath

> ().lastIndexOf(classPath)+1,

> files[k].getPath().length());

>

>

> That looks doomed for failure. Why don't you store

> the String that getPath() returns in a variable,

> seeing as how you use it several times... my bet is

> that you're NPE is coming from here ... but your

> compiler is already telling you. Could you please

> identify which line is 118?

It says line 118, in my compiler is

for(int k=0;k<files.length;k++){

which doesn't make that much sense.... and the code at

String filename=files[k].getPath().substring(files[k].getPath().lastIndexOf(classPath)+1, files[k].getPath().length());

works fine but it only gets the end of the directory.

But I'm pretty sure its using the File object in the jar because it doesn't crash at all in my compiler, Esclipse, it works perfectly fine. Is there another way of getting a list of directories in a jar?>

blackmagea at 2007-7-12 22:14:50 > top of Java-index,Java Essentials,Java Programming...
# 3

> It says line 118, in my compiler is

>

> for(int k=0;k<files.length;k++){

>

> which doesn't make that much sense....

files is null then. Which means that

File file=new File(userspath+firstpath+classPath+secondpath);

doesn't do what you think it does.

CeciNEstPasUnProgrammeura at 2007-7-12 22:14:50 > top of Java-index,Java Essentials,Java Programming...
# 4
Well I have directories like user/a/a/username, userb/c/username that I compiled inside my jar. How do I read those then?
blackmagea at 2007-7-12 22:14:50 > top of Java-index,Java Essentials,Java Programming...
# 5
> Well I have directories like user/a/a/username,> userb/c/username that I compiled inside my jar. How> do I read those then?As you already pointed out: getResource(). Inside JARs there are also no directories. A JAR isn't a file system.
CeciNEstPasUnProgrammeura at 2007-7-12 22:14:50 > top of Java-index,Java Essentials,Java Programming...
# 6

I know getResource returns it has a stream. But can that be used for checking if it exist? Because the program checks if something is in that directory and only loads it if there is something. And the directories are suppose to change by the user. So what might be there now may not be there 5 minutes later or something new might there.

blackmagea at 2007-7-12 22:14:50 > top of Java-index,Java Essentials,Java Programming...
# 7

> I know getResource returns it has a stream. But can

> that be used for checking if it exist?

Reading the API must be difficult:

"returns: A InputStream object or null if no resource with this name is found"

> Because the

> program checks if something is in that directory and

> only loads it if there is something. And the

> directories are suppose to change by the user.

How are you going to do that inside a JAR?

CeciNEstPasUnProgrammeura at 2007-7-12 22:14:50 > top of Java-index,Java Essentials,Java Programming...
# 8
>How are you going to do that inside a JAR?By unzipping it, adding files, and then re-zipping it.Thats why it changes and I need a way to check if files exist or not.
blackmagea at 2007-7-12 22:14:50 > top of Java-index,Java Essentials,Java Programming...
# 9

Doesn't sound like a very good idea to me. Jars aren't realy suited to volatile data and the ClassLoader isn't necessarilly going to react well to an attempt to replace one of it's jars (it may well keep it open).

As a general rule if a jar is created dynamically then any classloader pointing at it should be explicity created programatically (URLClassLoader). You'll get into all kinds of trouble trying to rewrite jars which may be on the class path.

malcolmmca at 2007-7-12 22:14:50 > top of Java-index,Java Essentials,Java Programming...