• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Jar files: Accessing files inside subdirectory (Java)

Empyrean

Limp Gawd
Joined
May 27, 2005
Messages
327
I have a very specific Java problem that is driving me nuts. Anyway, this is inside of an application I'm developing using the J2SE 5.0 JDK on a WinXP SP2 system.

The issue is that I have a single Jar file that I'm both reading configuration information from and writing information to. Now, one of the files I'm writing to this Jar I want to put inside a subdirectory, and I need to create this directory if it doesn't exist (which it won't the first time the application runs.) The process I am using is to create a File object and use the mkdir( ) function to create the folder. Then when I create the file object I use this directory handle in the constructor to create the file in that directory. And I add it to the Jar file and everything seems to work. But, when I call the entries( ) function on the Jar file and look at the Enumeration that is returned (and use the getName( ) on each entry), the one in the subfolder is listed as:

subfolder\file.cfg

and it has a backslash used as the directory deliminator. The parser I am using (and I must use this parser) requires a '/' as the directory deliminator. This has always worked up until now, because if from the operating system (outside of the program) if I use WinZip to add a subdirectory and file to the Jar file, then the Enumeration I get for that from the program is:

subfolder/file.cfg

If I view the contents of the Jar file in WinZip with files added both from within the program and externally, WinZip shows them the same way (the path field reads: subdirectory\ ), but the Java program interprets them differently. I am searching for a way to have the file I add inside the program to also be read as:

subfolder/file.cfg

I've tried a few different ideas (various constructors for the File object), obviously nothing has panned out yet. If anyone can offer any insight I would appreciate it greatly.
 
Take a look at what the File.seperatorChar is. I believe in windows it's the '\' character, which java will use to name things in the Jar file. This is a static final variable, so it's not something that can be changed.

Is your parser in the java code. You could just do this

FileName.replace(File.separatorChar,'/'); to make sure it is always a /.
 
Thanks for the help. The File.seperatorChar was '\' since I'm on a Windows system. It really sucks that they make that both static and final, but that led me to the answer. It isn't possible to solve this from outside of the parser. I found JarEntry objects don't have the restrictions that File objects do. So I could still create the File object in the file system without worrying about the directory path at that point.

I was able to get access to the parser, and there I found that by specifying the path when I create the JarEntry object I can choose the path structure I want. I take the file name I have as a string and then use

new JarEntry ( "subdirectory/" + l_Filename );

where l_Filename is a String containing the file name. This creates the appropriate entry that the parser does not have trouble when it tries to read the file back in.

So, I guess the question is, when will Windows grow up and use '/' as the path separator character?
 
Back
Top