java.nio.file.InvalidPathException: Illegal char <:> at index 2:

We Are Going To Discuss About java.nio.file.InvalidPathException: Illegal char <:> at index 2:. So lets Start this Java Article.

java.nio.file.InvalidPathException: Illegal char <:> at index 2:

  1. java.nio.file.InvalidPathException: Illegal char <:> at index 2:

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

  2. java.nio.file.InvalidPathException: Illegal char <:> at index 2:

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

Solution 1

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");

Original Author hunter Of This Content

Solution 2

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

Original Author Fego Of This Content

Solution 3

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());

Original Author techguy Of This Content

Solution 4

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);

Original Author SUDIP SINGH Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment