Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Runtime.getRuntime().exec()未打开正确的windows文件资源管理器_Java_Windows_Unicode_Unicode Normalization - Fatal编程技术网

Java Runtime.getRuntime().exec()未打开正确的windows文件资源管理器

Java Runtime.getRuntime().exec()未打开正确的windows文件资源管理器,java,windows,unicode,unicode-normalization,Java,Windows,Unicode,Unicode Normalization,我正在使用Java的Runtime.getRuntime().exec(字符串命令)打开指定文件的windows文件资源管理器。为此,我使用以下命令: 探索者路径打开 除非路径包含一些同样以NFD(规范化形式规范化分解)规范化形式存在的unicode字符,否则这种方法几乎一直有效 让我举个例子。我有以下命令: 浏览器C:\Test\foĕlder 我的本地文件系统中有所有“C驱动器”、“测试”和“文件夹”文件夹fỏlder'conatins unicode字符“ỏ”为NFD格式。此外,我还要

我正在使用Java的Runtime.getRuntime().exec(字符串命令)打开指定文件的windows文件资源管理器。为此,我使用以下命令:

探索者路径打开

除非路径包含一些同样以NFD(规范化形式规范化分解)规范化形式存在的unicode字符,否则这种方法几乎一直有效

让我举个例子。我有以下命令:

浏览器C:\Test\foĕlder

我的本地文件系统中有所有“C驱动器”、“测试”和“文件夹”文件夹fỏlder'conatins unicode字符“ỏ”为NFD格式。此外,我还要确保传递给exec方法的字符串也包含与这些文件夹中相同的unicode字符,即NFD格式

但这会在我的用户文件夹中打开“文档”文件夹,而不是打开“文件夹”。如果我创建了另一个文件夹,比如“fỏlderInNFC”,其中“ỏ”是NFC格式的,那么如果我创建了“explorer C:\Test\fỏlderInNFC”,那么它将打开所需的文件夹,即“fỏlderInNFC”

我也可以从windows命令提示符中看到相同的结果。我一个接一个地复制和粘贴了两条路径,可以看到相同的行为。 . 我还可以看到,命令提示符无法识别NFD格式中的“o”,但它能够识别NFC格式中的“o”


Runtime.getRuntime().exec()和cmd是否都不支持NFD unicode字符?如果没有,那么java中是否有其他方法可以打开包含NFD格式unicode字符的特定文件或文件夹的windows文件资源管理器?

您可以使用包含NFD unicode字符的路径名打开windows资源管理器,如下所示

假设在
C:\temp
中有一个文件夹
földer
(如果下面的“folder~1”后面看起来是乱码,可能与浏览器使用的字体有关)

编辑打开文件夹并选择一个文件,您可以使用该文件夹的短名称。您可以使用检索的文件夹的短名称。在下面找到第二个问题的简短片段。;-)

import com.sun.jna.Native;
导入com.sun.jna.platform.win32.Kernel32;
导入java.io.File;
导入java.io.IOException;
公共类OpenFolderAndSelectItem{
公共静态字符串getShortPathName(字符串路径){
字符[]结果=新字符[256];
GetShortPathName(路径、结果、结果、长度);
返回Native.toString(结果);
}
//添加异常处理,仅用于示例
公共静态void main(字符串[]args)引发IOException{
字符串nfdFolderName=“c:\\temp\\fo\u0308lder”;
System.out.println(“NFD格式中带有unicode字符的foldername:”
+nfdFolderName);
文件文件=新文件(nfdFolderName);
mkdir()文件;
//在该文件夹中创建一些虚拟文件

对于(char c='0';c,它不会以这种方式将参数传递给可执行文件,请使用ProcessBuilder Instead。我也使用了它。它提供了与上述相同的输出。谢谢,它工作了!但是如果我也想选择该文件/文件夹,那么?我的意思是类似于“explorer/select,c:\Test\fỏlder”
16/12/2015  15:39    <DIR>          FOLDER~1     földer
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);
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();
    }
}