Exception in thread “main” java.nio.file.InvalidPathException: Illegal char <:> at index 2:

前端 未结 4 1001
礼貌的吻别
礼貌的吻别 2020-12-15 16:57

I have to copy classpath resource from one package to another.

My program is:

    public static void main(String[] args) throws IOException, URISynta         


        
相关标签:
4条回答
  • 2020-12-15 17:42

    I have the same problem which I was facing from the past two days and finally, I got it Space causes such problem try to solve

    var fileName=YourFileName.trim();
    Path filePath = Paths.get(dirPathStr, fileName);
    
    0 讨论(0)
  • 2020-12-15 17:54

    Try this:

    Path path = new File(getClass().getResource("/<path to the image in your build/classes folder>").getFile()).toPath();

    to get the correct path. Worked for me after several hours trying to find out why I couldn't get the file from the jar. THis works for NetBeans 8.02

    0 讨论(0)
  • 2020-12-15 18:01

    problem is that Paths.get() doesnt expect that kind of value which is generated from uri.getPath().

    Solution:

    URI uri = ClassLoader.getSystemResource("com/stackoverflow/json").toURI();
    String mainPath = Paths.get(uri).toString();
    Path path = Paths.get(mainPath ,"Movie.class");
    
    0 讨论(0)
  • 2020-12-15 18:02

    I had the same issue and got the exception, noticed there was a space in the filename, so I had to trim it. After that, the issue is resolved.

    Path filePath = Paths.get(dirPathStr, newFileName.trim());
    
    0 讨论(0)
提交回复
热议问题