当前位置: 主页 > JAVA语言

java classpath 目录-java classpath等于点

发布时间:2023-02-09 11:20   浏览次数:次   作者:佚名

Java中有多种加载资源的方式:

这。 得到类()。 获取资源(资源名称)

除非它以“/”开头,否则在与此相同的包中查找文件。

Thread.currentThread().getContextClassLoader().getResource(资源名称)

ClassLoader可以共享java classpath 目录,创建的线程可以使用Thread.setContextClassLoader互相加载对象的资源。

如果没有设置,它是其父线程的类加载器。

ClassLoader.getSystemClassLoader().getResource(资源名称)

需要以 root 开头的完整路径

代码示例

项目结构:

java classpath等于点_java 读取classpath_java classpath 目录

package com.logicbig.example;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
public class ClassA {
    private static final String line = "-----------------------------------------";
    private void loadResource (String resource) throws IOException {
        URL u = this.getClass().getResource(resource);
        loadResourceByUrl(u, resource);
    }
    private void loadResourceWithContextLoader (String resource) throws IOException {
        URL u = Thread.currentThread().getContextClassLoader().getResource(resource);
        loadResourceByUrl(u, resource);
    }
    private void loadResourceWithSystemClassLoader (String resource) throws IOException {
        URL u = ClassLoader.getSystemClassLoader().getResource(resource);
        loadResourceByUrl(u, resource);
    }
    private void loadResourceByUrl (URL u, String resource) throws IOException {
        System.out.println("-> attempting input resource: "+resource);
        if (u != null) {
            String path = u.getPath();
            path = path.replaceFirst("^/(.:/)", "$1");
            System.out.println("    absolute resource path found :\n    " + path);
            String s = new String(Files.readAllBytes(Paths.get(path)));
            System.out.println("    file content: "+s);
        } else {
            System.out.println("    no resource found: " + resource);
        }
    }
    public static void main (String[] args) throws IOException {
        ClassA a = new ClassA();
        System.out.println(line+"\nusing this.getClass().getResource\n"+line);
        a.loadResource("test-pkg-resource.txt");
        a.loadResource("/test-pkg-resource.txt");
        a.loadResource("root-resource.txt");
        a.loadResource("/root-resource.txt");
        System.out.println(line+"\n using current thread context loader\n"+line);
        a.loadResourceWithContextLoader("test-pkg-resource.txt");
        a.loadResourceWithContextLoader("/test-pkg-resource.txt");
        a.loadResourceWithContextLoader("root-resource.txt");
        a.loadResourceWithContextLoader("/root-resource.txt");
        System.out.println(line+"\n using ClassLoader.getSystemClassLoader()\n"+line);
        a.loadResourceWithSystemClassLoader("test-pkg-resource.txt");
        a.loadResourceWithSystemClassLoader("/test-pkg-resource.txt");
        a.loadResourceWithSystemClassLoader("root-resource.txt");
        a.loadResourceWithSystemClassLoader("/root-resource.txt");
    }
}

如果使用 IntelliJ IDE 15.0.2 和 JDK 1.8 环境,资源将被复制到输出目录。 资源将在运行时复制到资源目录。

java classpath等于点_java 读取classpath_java classpath 目录

输出

-----------------------------------------
using this.getClass().getResource
-----------------------------------------
-> attempting input resource: test-pkg-resource.txt
    absolute resource path found :
    C:/load-resource/out/production/load-resource/com/logicbig/example/test-pkg-resource.txt
    file content: test file, local to package
-> attempting input resource: /test-pkg-resource.txt
    no resource found: /test-pkg-resource.txt
-> attempting input resource: root-resource.txt
    no resource found: root-resource.txt
-> attempting input resource: /root-resource.txt
    absolute resource path found :
    C:/load-resource/out/production/load-resource/root-resource.txt
    file content: root test file
-----------------------------------------
 using current thread context loader
-----------------------------------------
-> attempting input resource: test-pkg-resource.txt
    no resource found: test-pkg-resource.txt
-> attempting input resource: /test-pkg-resource.txt
    no resource found: /test-pkg-resource.txt
-> attempting input resource: root-resource.txt
    absolute resource path found :
    C:/load-resource/out/production/load-resource/root-resource.txt
    file content: root test file
-> attempting input resource: /root-resource.txt
    no resource found: /root-resource.txt
-----------------------------------------
 using ClassLoader.getSystemClassLoader()
-----------------------------------------
-> attempting input resource: test-pkg-resource.txt
    no resource found: test-pkg-resource.txt
-> attempting input resource: /test-pkg-resource.txt
    no resource found: /test-pkg-resource.txt
-> attempting input resource: root-resource.txt
    absolute resource path found :
    C:/load-resource/out/production/load-resource/root-resource.txt
    file content: root test file
-> attempting input resource: /root-resource.txt
    no resource found: /root-resource.txt

命令行运行

C:\load-resource>javac com/logicbig/example/ClassA.java

java classpath 目录_java classpath等于点_java 读取classpath

跑步

java classpath 目录_java classpath等于点_java 读取classpath

我们可以看到和idea中的输出是一致的,因为相当于class文件的资源的路径没有改变。

2.加载Maven资源

Maven项目资源放在:src/main/resources目录下。

java classpath 目录_java 读取classpath_java classpath等于点

输出

C:\load-resource>mvn exec:java -Dexec.mainClass="com.logicbig.example.ClassA"
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building load-resource 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.4.0:java (default-cli) @ load-resource ---
-----------------------------------------
using this.getClass().getResource
-----------------------------------------
-> attempting input resource: test-pkg-resource.txt
    no resource found: test-pkg-resource.txt
-> attempting input resource: /test-pkg-resource.txt
    absolute resource path found :
    C:/load-resource/target/classes/test-pkg-resource.txt
    file content: test file, local to package
-> attempting input resource: root-resource.txt
    no resource found: root-resource.txt
-> attempting input resource: /root-resource.txt
    absolute resource path found :
    C:/load-resource/target/classes/root-resource.txt
    file content: root test file
-----------------------------------------
 using current thread context loader
-----------------------------------------
-> attempting input resource: test-pkg-resource.txt
    absolute resource path found :
    C:/load-resource/target/classes/test-pkg-resource.txt
    file content: test file, local to package
-> attempting input resource: /test-pkg-resource.txt
    no resource found: /test-pkg-resource.txt
-> attempting input resource: root-resource.txt
    absolute resource path found :
    C:/load-resource/target/classes/root-resource.txt
    file content: root test file
-> attempting input resource: /root-resource.txt
    no resource found: /root-resource.txt
-----------------------------------------
 using ClassLoader.getSystemClassLoader()
-----------------------------------------
-> attempting input resource: test-pkg-resource.txt
    absolute resource path found :
    C:/load-resource/target/classes/test-pkg-resource.txt
    file content: test file, local to package
-> attempting input resource: /test-pkg-resource.txt
    no resource found: /test-pkg-resource.txt
-> attempting input resource: root-resource.txt
    absolute resource path found :
    C:/load-resource/target/classes/root-resource.txt
    file content: root test file
-> attempting input resource: /root-resource.txt
    no resource found: /root-resource.txt   

目标资源是什么样的

java classpath等于点_java classpath 目录_java 读取classpath

maven中的资源加载

maven中如何修改资源目录?

可以在 pom.xml 文件中定义标签以覆盖默认资源路径。

如下配置,我们将java和resources目录定义为资源目录。


    .....
    
      
        src/main/java
      
      
        src/main/resources
      
    
    .....
    

英文原文:

如果您觉得本文对您有帮助java classpath 目录,请点赞评论,欢迎关注我,我会努力创作出更多更好的文章。