Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I unmangle Windows filenames in Java?

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

like image 592
skiphoppy Avatar asked Mar 25 '10 18:03

skiphoppy


1 Answers

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.

like image 115
Chris Avatar answered Oct 02 '22 12:10

Chris