How do I rewrite Windows file names in Java?

From Java, I extract the executable to the location indicated by File.createTempFile (). When I try to run my executable, my program freezes when it tries to read the first line of output.

I found that if I try to run the same extracted executable from another program, it will work 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 a hang.

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

(And since I always like problems with the language and design of the API, is there any reason Java File.createTempFile () and java.io.tmpdir should evaluate garbled file names?)

+6
java windows filenames name-mangling
source share
2 answers

You can use getCanonicalPath() to get the extended path. For example:.

 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 I assume that it will work similarly on other Windows operating systems.

See @raviaw answer to the second question.

+10
source share

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

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

Assuming that the newly created Java virtual machine uses an environment variable to obtain the temporary location of the folder, this is not a VM error that causes directories to run.

And even if you try to use System.getenv () to get a 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" (depending on your question, I assume this);
  • If the file is "prog.exe", if it is not used by any other program (possibly an antivirus);
  • Checking the health of your computer (this will be a very critical error for any application that is not a web application and that needs temporary files).
+3
source share

All Articles