Question

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?)

Was it helpful?

Solution

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.

OTHER TIPS

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).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top