java - 文件: URI corresponding to a Windows path name look like?应该如何

标签 java file url jar

我正在尝试在代码中动态加载 MySQL jar,但我不确定 Windows 路径名的格式。我在下面使用的是正确的,用于从拇指驱动器加载 .jar 吗?

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

现在,这与您在 Java 教程中看到的传统路径不同:

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

对于答案,我正在寻求澄清;我想我只是对“jar:file:”和“!”感到困惑。在最后。

最佳答案

File 对象上使用 toURI() 方法应该可以解决问题:

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

关于java - 文件: URI corresponding to a Windows path name look like?应该如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7151877/

相关文章:

c# - 在 C# 中验证 URL 的一种比 try-catch 更好的方法?

java - 没有类型转换的不同类的数组列表?

windows - 如何复制/镜像文件,但仅作为文件长度为 0 的占位符?

linux - 是否有 "lock files"的标准 Linux 库?

javascript - 如何使用正则表达式在字符串中强制使用字符/特殊字符

javascript - 使用 href ssh ://url with command line options 从 html 打开终端

java - Android SDK 通过HTTP get获取图片

java - 哪种语言/框架适用于 HPC : Java/.Net/Delphi/C/C++/Objective-C?

network-programming - HttpURLConnection 实现

file - Dart 路径依赖项不起作用(跨多个项目)