문제

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

도움이 되었습니까?

해결책

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.

다른 팁

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).
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top