Is there a way to generate the 8.3 or 'short' (Windows) version of a file name in Java?

Deadly 提交于 2020-01-15 08:23:41

问题


In our application, we are allowing users to open files and directories.

Java 6 provides us with...

java.awt.Desktop.getDesktop().open(file);

which works great. However, since we need to ensure Java 5 compatibility, we also implement a method of opening files by calling the start command in cmd.exe...

String command = "cmd.exe start ...";
Runtime.getRuntime().exec(command);

This is where the problem shows up. It seems that the start command can only handle 8.3 file names, which means that any non-short (8.3) file/directory names cause the start command to fail.

Is there an easy way to generate these short names? Or any other workarounds?


回答1:


Try something like this

import java.io.IOException;

class StartExcel {
    public static void main(String args[])
        throws IOException
    {
        String fileName = "c:\\temp\\xls\\test2.xls";
        String[] commands = {"cmd", "/c", "start", "\"DummyTitle\"",fileName};
        Runtime.getRuntime().exec(commands);
    }
}

It's important to pass a dummy title to the Windows start command where there is a possibility that the filename contains a space. It's a feature.




回答2:


Try this: http://dolf.trieschnigg.nl/eightpointthree/eightpointthree.html




回答3:


Or you could try:

Runtime.getRuntime().exec(
  new String[] { System.getenv("windir") + "\\system32\\rundll32.exe",
    "shell32.dll,ShellExec_RunDLL", "http://www.stackoverflow.com" });

Source: http://www.rgagnon.com/javadetails/java-0014.html



来源:https://stackoverflow.com/questions/972559/is-there-a-way-to-generate-the-8-3-or-short-windows-version-of-a-file-name-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!