0
votes

I am using Java's Runtime.getRuntime().exec(String command) to open windows file explorer for a specified file. For that i am using the command :

explorer pathToOpen

This works for almost all the time unless the path contains some unicode characters that too in NFD(Normalization Form Canonical Decomposition) normalized form.

Let me take an example here. I am having the following command :

explorer C:\Test\földer

I have all 'C drive', 'Test' and 'földer' folders in my local file system. 'földer' conatins unicode character 'ö' in NFD form. Also I make sure that String which I am passing to exec method also contain same unicode character as in these folders i.e. in NFD form.

But this opens 'Documents' folder inside my user folder instead of opening 'földer'. If I create another folder lets say 'földerInNFC' where 'ö' is in NFC form, then if i do "explorer C:\Test\földerInNFC", then it opens the desired folder which is 'földerInNFC'.

I could see the same result from windows command prompt as well. I copied and pasted both the paths one by one and could see the same behavior. Here is a snippet of my command prompt. Also I could see that command prompt is not able to recognize 'ö' in NFD form but it is able to recognize 'ö' in NFC form.

Does both Runtime.getRuntime().exec() and cmd not support NFD unicode characters? If no, then is there another way in java to open windows file explorer for a specific file or folder which contains unicode characters in NFD form?

1
it doesn't pass parameters to executable this way, use ProcessBuilder insteadjmj
I have used that also. It gives the same output as above mentioned.rachit

1 Answers

0
votes

You can open the Windows Explorer with pathnames containing NFD unicode characters like below

assume in C:\temp a folder földer (if below after "FOLDER~1" it looks garbled it might be related to the font used by your browser)

16/12/2015  15:39    <DIR>          FOLDER~1     földer

snippet to open C:\temp\földer (contains NFD unicode characters)

String nfdPath = "fo\u0308lder"; // földer in NFD unicode
File file = new File("c:/temp/" + nfdPath);
// uncomment the next line if you want that folder to be created first
// file.mkdir();
java.awt.Desktop.getDesktop().open(file);

edit To open the folder and select a file you can use the shortname of that folder. The shortname of that folder you can retrieve by using JNA. Find below a short snippet for your second question. ;-)

import com.sun.jna.Native;
import com.sun.jna.platform.win32.Kernel32;
import java.io.File;
import java.io.IOException;

public class OpenFolderAndSelectItem {

    public static String getShortPathName(String path) {
        char[] result = new char[256];
        Kernel32.INSTANCE.GetShortPathName(path, result, result.length);
        return Native.toString(result);
    }

    // add exception handling, left out only for the example
    public static void main(String[] args) throws IOException {
        String nfdFolderName = "c:\\temp\\fo\u0308lder";
        System.out.println("foldername with unicode character in NFD form: "
            + nfdFolderName);
        File file = new File(nfdFolderName);
        file.mkdir();
        // create some dummy files in that folder
        for (char c = '0'; c <= '9'; c++) {
            new File(nfdFolderName + "/file" + c).createNewFile();
        }
        // get the 8.3 short name of the folder
        String nfdFolderShortName = getShortPathName(nfdFolderName);
        // open the folder c:\temp\földer and select the file 'file4'
        ProcessBuilder process = new ProcessBuilder();
        process.command("explorer", "/select," + nfdFolderShortName
            + "\\file4");
        process.inheritIO();
        process.start();
    }
}

For the possible next question "How can I select multiple files": I believe the only way would be to write your own library/tool implementing SHOpenFolderAndSelectItems.