views:

80

answers:

2

From Java, I'm extracting an executable into a location specified using File.createTempFile(). When I try to run my executable, my program hangs when it tries to read the first line of output.

I have discovered that if I try to run the same extracted executable from another program, it works if I specify the directory as C:\Documents and Settings\username\Local Settings\Temp\prog.exe. But if I specify the directory as C:\DOCUME~1\USERNA~1\LOCALS~1\Temp\prog.exe I get the hang.

Is there a way to unmangle the tilde filename in my program so I can specify a directory name that will work?

(And since I always like addressing the language and API design issues, is there any reason why Java File.createTempFile() and java.io.tmpdir have to evaluate to mangled filenames?)

+5  A: 

You can use getCanonicalPath() to get the expanded path. E.g.:

try
{
  File file = File.createTempFile("abc", null);
  System.out.println(file.getPath());
  System.out.println(file.getCanonicalPath());
}
catch (IOException e) {}

... produces ...

C:\DOCUME~1\USERNA~1\LOCALS~1\Temp\abc49634.tmp
C:\Documents and Settings\username\Local Settings\Temp\abc49634.tmp

I tested this on XP, but assume it would work similarly on other Windows operating systems.

See @raviaw's answer to your second question.

Chris
Better answer than mine, although I doubt this is his problem.
Ravi Wallau
That does indeed seem to de-mangle the filenames! Somehow it still hangs when I run, though. I don't understand why, yet, but working on it...
skiphoppy
Ah hah! I had to call close() on the OutputStream I used when I extracted the executable.Turns out on my test, I was inadvertantly passing the mangled filename for an executable still in use (and not closed()) by my application, and passing a full filename for a no-longer used previous extraction.
skiphoppy
+3  A: 

Wow, I never saw that. The fact is that the environment variable %TEMP% returns a mangled name (this is from my computer):

TEMP=C:\DOCUME~1\raviw\LOCALS~1\Temp
TMP=C:\DOCUME~1\raviw\LOCALS~1\Temp

Assuming that a newly create java VM uses the environment variable to get the temporary folder location, it is not VM's fault that the directories are coming mangled.

And even if you try to use System.getenv() to get the temporary folder, you will still have the same problem.

I would make sure that:

  • The problem is not caused by the fact that you have a directory called "prog.exe" (based on your question, I am assuming this);
  • If the file is "prog.exe", if it was not in use by any other program (an antivirus, maybe);
  • Checking if your computer is sane (this would be a very critical bug for any application that is not a web application and that need temporary files).
Ravi Wallau
+1 for the answer to his second question which I forgot to address.
Chris
Turns out it was in use by another program ... the application I was writing. Which had extracted it, and never called close(). So all calls to try to run it, whether from my test or from the application, were hanging indefinitely waiting for that close() and the resulting flush to disk.
skiphoppy