问题
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