简体   繁体   English

等价于Jar中的this.getClass()。getClassLoader()。getResource(“。”)。toURI()

[英]Equivalent of this.getClass().getClassLoader().getResource(“.”).toURI() from within a Jar

I'm trying to achieve a way to obtain the base path of the current classloader when runnning from within a jar. 我正在尝试找到一种从jar中运行时获取当前类加载器的基本路径的方法。

I mean programatically, I already know it should have the shape of "jarPath+jarFile.jar!/" 我的意思是以编程方式,我已经知道它的形状应为“ jarPath + jarFile.jar!/”

Unlike file system's call, getResource(".") or .getResource("/") do not work from inside the jar. 与文件系统的调用不同,getResource(“。”)或.getResource(“ /”)在jar内无法正常工作。

Ideally it should be an abstract solution for any file provider, so something like: 理想情况下,它应该是任何文件提供程序的抽象解决方案,例如:

Path BASE_PATH = Paths.get(...getResource("").toURI())

which could return the correct root path for both jars and file system so I can use relative urls to my resources without having to do any conditional statements and url manual string parsing/build. 可以为jar和文件系统返回正确的根路径,因此我可以对资源使用相对的url,而不必执行任何条件语句和url手动字符串解析/构建。

You should be able to find out the path of the jar and or target folder containing you class or any resource by using this code: 通过使用以下代码,您应该能够找到包含您的类或任何资源的jar和/或目标文件夹的路径:

package com.stackoverflow.test;

import java.net.URL;
import java.nio.file.Paths;

public class ClassPathUtils {

  public static String getBasePath(String jarPath) {
    String path = getJarPathFromClass(jarPath);
    if (path == null) {
      return null;
    }

    if (path.startsWith("jar:")) {
      path = path.substring("jar:".length());
    }
    if (path.startsWith("file:")) {
      path = path.substring("file:".length());
    }
    if (path.endsWith(jarPath)) {
      path = path.substring(0, path.length()-jarPath.length());
    }

    return path;
  }
  public static String getBasePath(Class clazz) {
    return getBasePath(classNameDotClass(clazz));
  }
  private static String classNameDotClass(Class clazz) {
    return clazz.getName().replaceAll("\\.", "/") + ".class";
  }
  private static String getJarPathFromClass(String resource) {
    final URL url = ClassPathUtils.class.getClassLoader().getResource(resource);
    return url == null ? null : url.toString();
  }
  public static void main(String[] args) {
    //System.out.println(Paths.get(ClassPathUtils.getBasePath("."))); // doesn't work in a jar
    System.out.println(Paths.get(ClassPathUtils.getBasePath(ClassPathUtils.class)));

    System.out.println(Paths.get(ClassPathUtils.getBasePath("fonts/atcitadelscript.ttf"))); // any classpath resource

    System.out.println(Paths.get(ClassPathUtils.getBasePath(String.class))); // actually finds rt.jar

  }
}

If you run this code from your IDE, or from maven, it will give you the paths to target/classes for your own resources , or the path to a jar for other resources (Eg String.class). 如果从IDE或maven运行此代码,它将为您提供自己资源的目标/类的路径,或其他资源的jar的路径(例如String.class)。 If you call it from a jar, it will always tell you the path of the jar file. 如果从jar调用它,它将始终告诉您jar文件的路径。

run from IDE: 从IDE运行:

/home/alexander/projects/stackoverflow/stuff/target/classes
/home/alexander/projects/stackoverflow/stuff/target/classes
/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/rt.jar!`

run from JAR: 从JAR运行:

/home/alexander/projects/stackoverflow/stuff/target/test-stuff-0.1-SNAPSHOT.jar!
/home/alexander/projects/stackoverflow/stuff/target/test-stuff-0.1-SNAPSHOT.jar!
/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/rt.jar!

Is that what you're looking for? 那是您要找的东西吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 this.getClass()。getClassLoader()。getResource()==异常 - this.getClass().getClassLoader().getResource() == Exception this.getClass()。getClassLoader()和ClassLoader - this.getClass().getClassLoader() and ClassLoader Java getClass()。getClassLoader()。getResource(path)在Maven阴影.jar中失败 - Java getClass().getClassLoader().getResource(path) fails inside Maven shaded .jar Java:getClass().getResource().toURI() 与 getClass().getResourceAsStream() - Java : getClass().getResource().toURI() Vs getClass().getResourceAsStream() this.getClass()。getClassLoader()。getResourceAsStream()在RCP产品中不起作用 - this.getClass().getClassLoader().getResourceAsStream()is not working in RCP product this.getClass()。getClassLoader()。getResourceAsStream始终返回null - this.getClass().getClassLoader().getResourceAsStream always returning null 如何使用this.getClass()。getResource(String)? - How to use this.getClass().getResource(String)? Openshift this.getClass()。getResource()路径可能不正确 - Openshift this.getClass().getResource() path probably not correct this.getClass()。getResource(“”)。getPath()返回错误的路径 - this.getClass().getResource(“”).getPath() returns an incorrect path getClass().getClassLoader().getResource() 和 getClass.getResource() 的区别? - Difference between getClass().getClassLoader().getResource() and getClass.getResource()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM