This is not a tag line!
Beware of File.createTempFile
By default, File.createTempFile (in Java, of course) creates a temporary file in the directory identified by the system property java.io.tmpdir. The problem is that on OS X, that directory can be quite weird. For example, on my system, it currently is:
scala> System.getProperties().getProperty("java.io.tmpdir")
res0: java.lang.String = /var/folders/ya/yaNATrKPGFu2HuSOWxSIu++++TI/-Tmp-/
Notice the ++++ in one of the directory names? Well, these little characters can later cause headaches! The reason, you ask? Simple, if that temporary file name is ever used in a URL, then those plus signs will be interpreted as spaces and whatever code is using a URL to find your temporary file will not find it! So beware!
Possible solutions include passing -Djava.io.tmpdir="whatever" when you launch your Java process, or use the createTempFile(String prefix, String suffix, File directory) version of File.createTempFile and pass it a directory that doesn’t contain an + sign in its name!
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.
| Print article | This entry was posted by Chris on September 4, 2009 at 20:28, and is filed under apple, java. Follow any responses to this post through RSS 2.0. Both comments and pings are currently closed. |
Comments are closed.


about 6 months ago
Does File.toURI().toURL() not escape it correctly?
about 6 months ago
It might, I haven’t tried… The problem is when you don’t control the code. I got hit by the issue trying to run tests on AS 5.1 via cargo. JBoss’ config is put in the default temp dir by cargo and MC tries to load its configuration using FileURLConnection which doesn’t properly escape the file name, resulting in MC not finding its bootstrapping file (because FileURLConnection looks for a file with spaces in its name instead of leaving the + chars as is). I haven’t looked too deep into the issue so I don’t know who’s to blame here: cargo or MC…
about 6 months ago
Hey, you probably found why WCI tests on JBoss are failing on my MBP. I’ll try to fix it by setting the java.io.tmpdir in surefire settings (because tests are forked thus we cannot use MAVEN_OPTS). Perhaps we could put java.io.tmpdir=${project.build.directory} /tmp
about 6 months ago
Actually, Matt has committed a fix for it. You can configure cargo to tell it where the server config to boot from should be created. I haven’t tried it yet, but will check it out tomorrow.