首页 文章

加载jar中包含的资源

提问于
浏览
21

在我的应用程序中,我以这种方式加载资源:

WinProcessor.class.getResource("repository").toString();

这给了我:

file:/root/app/repository   (and I replace "file:" with empty string)

当我从IDE运行我的应用程序时,这工作正常,但是当我运行我的应用程序的jar时:

java -jar app.jar

路径变成:

jar:/root/app.jar!/repository

有什么方法可以解决这个问题吗?

我将使用“repository”目录名称来创建:

ConfigurationContext ctx = (ConfigurationContext) ConfigurationContextFactory.createConfigurationContextFromFileSystem(repositoryString, null);

以同样的方式,我将获得一个文件名(而不是一个目录),我将以这种方式使用它:

System.setProperty("javax.net.ssl.trustStore", fileNameString)

3 回答

  • 9

    听起来你正试图使用 FileInputStream 或类似的东西加载资源 . 不要这样做:而不是调用 getResource ,而是调用getResourceAsStream并从中读取数据 .

    (您可以从URL加载资源,但调用 getResourceAsStream 会更方便 . )

    编辑:看到你的更新答案后,似乎其他代码依赖于文件系统中物理单个文件中的数据 . 因此,答案不是首先将其捆绑在jar文件中 . 你可以检查它是否是非常hacky的IMO .

  • 5

    使用 java -jar app.jar 运行代码时,java仅使用JAR文件清单中定义的类路径(即 Class-Path 属性) . 如果该类位于 app.jar 中,或者该类位于JAR清单的 Class-Path 属性中设置的类路径中,则可以使用以下代码段加载该类,其中 className 是完全限定的类名 .

    final String classAsPath = className.replace('.', '/') + ".class";
    final InputStream input = ClassLoader.getSystemResourceAsStream( path/to/class );
    

    现在,如果该类不是JAR的一部分,并且它不是't in the manifest',那么类加载器将找不到它 . 相反,你可以使用 URLClassLoader ,小心处理windows和Unix / Linux / MacOSX之间的差异 .

    // the class to load
    final String classAsPath = className.replace('.', '/') + ".class";
    
    // the URL to the `app.jar` file (Windows and Unix/Linux/MacOSX below)
    final URL url = new URL( "file", null, "///C:/Users/diffusive/app.jar" );
    //final URL url = new URL( "file", null, "/Users/diffusive/app.jar" );
    
    // create the class loader with the JAR file
    final URLClassLoader urlClassLoader = new URLClassLoader( new URL[] { url } );
    
    // grab the resource, through, this time from the `URLClassLoader` object
    // rather than from the `ClassLoader` class
    final InputStream input = urlClassLoader.getResourceAsStream( classAsPath );
    

    在这两个示例中,您将需要处理异常,以及如果无法找到资源,则输入流为 null 的事实 . 另外,如果你需要将 InputStream 变成 byte[] ,你可以使用Apache的公共 IOUtils.toByteArray(...) . 并且,如果您想要 Class ,则可以使用类加载器的 defineClass(...) 方法,该方法接受 byte[] .

    您可以在Diffusive源代码中的ClassLoaderUtils类中找到此代码,您可以在SourceForge上找到该代码,网址为github.com/robphilipp/diffusive

    以及在RestfulDiffuserManagerResource.createJarClassPath(...)中从相对和绝对路径创建Windows和Unix / Linux / MacOSX的URL的方法

  • 35

    构造一个 URL ,然后可以使用openStream方法加载资源(甚至在jar文件中) .

相关问题