java エラー「Exception in thread “main” java.nio.file.InvalidPathException: Illegal char at index 3」が発生した場合の対処法

  • 作成日 2022.09.22
  • 更新日 2022.10.27
  • java
java エラー「Exception in thread “main” java.nio.file.InvalidPathException: Illegal char <?> at index 3」が発生した場合の対処法

javaで、エラー「Exception in thread “main” java.nio.file.InvalidPathException: Illegal char at index 3」が発生した場合の対処法を記述してます。

環境

  • OS windows11 home
  • java 17.0.2

エラー内容

以下のコードで発生。

import java.nio.file.Path;

public class App {
    public static void main(String[] args) throws Exception {

        Path p = Path.of("C:/?/java");        
    
        System.out.println(p);

    }
}

エラー全文

Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <?> at index 3: C:/?/java
        at java.base/sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
        at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
        at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
        at java.base/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92)
        at java.base/sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:232)
        at java.base/java.nio.file.Path.of(Path.java:147)
        at App.main(App.java:6)

原因

パスに使用できない文字を使用しているため

解決方法

使用できる文字に変更する