Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: Novice Newsgroups: comp.lang.java.programmer Subject: Resource confusion Date: Sat, 28 Jan 2012 17:47:48 +0000 (UTC) Organization: Your Company Lines: 82 Message-ID: NNTP-Posting-Host: /kSec8Zpu/mSG50nCPQbWQ.user.speranza.aioe.org X-Complaints-To: abuse@aioe.org User-Agent: Xnews/5.04.25 X-Antivirus-Status: Clean X-Notice: Filtered by postfilter v. 0.8.2 X-Antivirus: avast! (VPS 120128-0, 2012-01-28), Outbound message Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:11626 I'm having trouble figuring out the best way of obtaining existing files for my program to use. My program, which is a game, wants to open an input file that drives the game. Any number of input files may exist but they'll typically be stored in the same directory, although the user may choose to put supplementary input files in different directories. I provide a preference dialog where the user can indicate the directory which will serve as the starting point for the file chooser that selects the input file to be used in playing the game. The program needs to work when I am testing in Eclipse and it also needs to work when the program is executing in a production environment. I expect that it will run via Java Web Start, a JNLP file, and a jar. (I'm using a self-signed test certificate for now and granting all-permissions via the JNLP file.) At the moment, my preferences dialog allows the user to choose a directory (or a directory and a file) that will serve as the starting point for the file chooser that will be used by the Open logic that opens the input file. The value of that starting point is being correctly stored by my program and can be changed successfully via the preferences dialog. But I'm not having any luck actually obtaining the file via my current code. Given that the file name may include accented letters, it seems necessary to decode it with UTF-8 so my code proceeds as follows: ========================================================================= String phraseListStartPoint = this.userPrefs.get (PHRASE_LIST_STARTPOINT_KEY, PHRASE_LIST_STARTPOINT_VALUE); URL phraseListURL = this.getClass().getResource(phraseListStartPoint); String encodedFileName = null; String decodedFileName = null; try { encodedFileName = phraseListURL.getFile(); decodedFileName = URLDecoder.decode(encodedFileName, "UTF-8"); } catch (NullPointerException npe_excp) { this.logger.logp(Level.WARNING, this.CLASS_NAME, METHOD_NAME, "Phrase list URL, " + phraseListStartPoint + ", does not exist. Using default directory instead."); } catch (UnsupportedEncodingException ue_excp) { this.logger.logp(Level.WARNING, this.CLASS_NAME, METHOD_NAME, "Failed to decode file name " + encodedFileName, ue_excp); System.exit(16); } File startPoint = null; if (decodedFileName != null) startPoint = new File(decodedFileName); else startPoint = new File(System.getProperty("user.home")); /* Launch the file chooser, open the file, read it..... */ ========================================================================= This code inevitably reveals that phraseListURL is null, even though the value of PHRASE_LIST_STARTPOINT_VALUE is a path found on my file system such as C:\Games\PhraseLists. Clearly, this.getClass().getResource(phraseListStartPoint) is failing to work as I would like. I've tried reading the API on the getResource() method of the Class class but I'm not following it. I know nothing about class loaders or which one is the right one to use when. I'm not even sure I should be using an approach like this to access a file that is in the file system. I might bundle a few sample input files into the jar when I make this game publicly accessible via Java Web Start but I mean those only to be examples that players can use to build their own input files. Therefore, I don't think they'll be accessing their own files from within the jar but rather from the file system on their own computer. So I need the best way to code the access to this file regardless of whether it is in a jar or on the file system locally. Can anyone get me going in the right direction on this? Maybe a few general remarks about best practices and then suggestions on where to go to find the exact techniques I'll need? -- Novice