How should a file: URI corresponding to a Windows path name look like?

被刻印的时光 ゝ 提交于 2021-02-11 07:01:47

问题


I am trying to load a MySQL jar dynamically in code but I am unsure about the format of the Windows path name. Is what I am using below correct, for loading a .jar from a thumbdrive?

URL u = new URL("jar:file:G:/mysql-connector-java-5.1.15.jar!/");
URLClassLoader ucl = new URLClassLoader(new URL[] { u });

Now, this is not the same as the traditional path that you see in Java tutorials:

URL url = new URL("file:/g:/mysql-connector-java-5.1.15.jar");

For the answer, I am looking for clarification; I guess I am just confused by the "jar:file:" thing and also the "!" at the end.


回答1:


Using toURI() method on the File object should do the trick:

final URL u = new File("g:/something.jar").toURI().toURL();
URLClassLoader ucl = new URLClassLoader(new URL[] { u });


来源:https://stackoverflow.com/questions/7151877/how-should-a-file-uri-corresponding-to-a-windows-path-name-look-like

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!