且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Java命令行选项中将资源文件夹添加到类路径

更新时间:2023-11-24 21:31:10

在您的源代码中,使用以下构造:

In your source code, use the following construct:

package com.foo.package;

public class Example {
    public void loadResourceExample) {
         Example.class.getResource("/cde/FGH/SomeRandomFile.xml");
         Example.class.getResource("relative/example.xml");
    }
}

第一行将从类路径中提到的任何文件夹或jar中加载资源"/cde/FGH/SomeRandomFile.xml",因此在示例命令行中, D:\ WorkSpace \ Testspace \ MyProject \ target \例如,可以扫描classes \ cde \ FGH \ SomeRandomFile.xml .

The first line will load resource "/cde/FGH/SomeRandomFile.xml" from any folder or jar mentioned in the classpath, so in your example command line, D:\WorkSpace\Testspace\MyProject\target\classes\cde\FGH\SomeRandomFile.xml would for example be scanned.

第二行考虑了您的类的包名称,因此,它将例如在 D:\ WorkSpace \ Testspace \ myProject \ dependency \ somedep.jar 中检查条目/com/foo/package/relative/example.xml.

The second line takes the package name of your class into account, so, it would for example check at D:\WorkSpace\Testspace\myProject\dependency\somedep.jar for entry /com/foo/package/relative/example.xml.

请注意,-cp参数中的*语法将采用指定文件夹中的每个 JAR 并将其包含在类路径中.这就是全部.

Note that the * syntax in the -cp parameter will take every JAR inside the stated folder and include it in the classpath. That's all it does.

如果您不使用 getResource ,则实际上取决于当前工作目录",并且-cp参数不会有任何作用.

If you are not using the getResource, you're dependent on the 'current working directory' which can be anything, really, and the -cp parameter wouldn't have any effect.