4月 12, 2021

Java Paths.get() cross platform issue

 Original code:


This would work on Unix platform, the returned path is "/home/..../xyz.xml"


However on Windows, it would return "invalid" path "/C:/.../xyz.xml", which is causing Exception from the get().


Solution in bugtracker suggested

1. Use toURI() method to convert getResource() result to URI, then use Paths.get()

2. Use Legacy File class.

https://bugs.openjdk.java.net/browse/JDK-8197918


沒有留言:

張貼留言