Maven包打开jar包内的文件(作为*File*)

发布于 2024-10-05 00:15:46 字数 288 浏览 1 评论 0原文

我需要打开一个 Maven jar 包内的文件。它是我使用的框架的模型配置,库类的构造函数需要传递 File 类型的对象。我可以使用类加载器获取配置文件的路径,没有任何问题。但是 - 不幸的是 - File 无法读取 jar 内的文件。所以我得到了java.io.FileNotFoundException。现在我正在寻找这个问题的解决方案。我的计划是解压模型配置文件并将其放在临时目录中。然而,在开始编码之前,我想了解是否有其他解决方案来解决像我这样的问题。

更新:我需要在运行时读取文件。

I need to open a file which is inside a maven jar package. It a model configuration for a framework I use and a constructor of a library class requires to pass a object of type File. I can get a path to a configuration file using class loader without any problems. But -- unfortunately -- File can not read a file inside jar. So I get java.io.FileNotFoundException. Now I looking for a solution for this problem. My plan is to decompress the model configuration file and place it in a temporary directory. However, before starting coding, I would like to learn if there is any other solution for such a problem like mine.

UPDATE: I need to read a file in runtime.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾北清歌寒 2024-10-12 00:15:46

将 jar 资源解压到文件中

  • 如果您是从 Maven 构建执行此操作,请使用

    dependency:unpack-dependency (如果 jar 是项目的 Maven 依赖项之一)

    <前><代码><插件>;
    org.apache.maven.plugins
    maven-dependency-plugin;
    <处决>
    <执行>
    解包依赖项
    <阶段>生成资源
    <目标>
    <目标>解包依赖项

    <配置>
    the.groupId
    the.artifactId
    <包含> **/path/to/your/resource.txt
    您想要它的位置



或使用

  • dependency:unpack (如果 jar 没有依赖项,但仍可作为 Maven 工件使用)

    <前><代码><插件>;
    org.apache.maven.plugins
    maven-dependency-plugin;
    <处决>
    <执行>
    解压
    <阶段>生成资源
    <目标>
    <目标>解包

    <配置>
    <工件项目>
    <工件项目>
    the.groupid;
    the.artifactid;
    <版本>the.version
    <类型>罐子
    您想要它的位置
    <包含> **/path/to/your/resource.txt




If you're doing it from a maven build, unpack the jar resource to a file using

  • dependency:unpack-dependencies (if the jar is one of the project's maven dependenies)

    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-dependency-plugin</artifactId>
     <executions>
       <execution>
        <id>unpack-dependencies</id>
        <phase>generate-resources</phase>
        <goals>
          <goal>unpack-dependencies</goal>
        </goals>
        <configuration>
          <includeGroupIds>the.groupId</includeGroupIds>
          <includeArtifactIds>the.artifactId</includeArtifactIds>
          <includes>**/path/to/your/resource.txt</includes>
          <outputDirectory>where/do/you/want/it</outputDirectory>
        </configuration>
      </execution>
     </executions>
    </plugin>
    

or use

  • dependency:unpack (if the jar is no dependency, but still available as a maven artifact)

    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-dependency-plugin</artifactId>
     <executions>
       <execution>
         <id>unpack</id>
         <phase>generate-resources</phase>
         <goals>
           <goal>unpack</goal>
         </goals>
         <configuration>
           <artifactItems>
             <artifactItem>
               <groupId>the.groupid</groupId>
               <artifactId>the.artifactid</artifactId>
               <version>the.version</version>
               <type>jar</type>
               <outputDirectory>where/do/you/want/it</outputDirectory>
               <includes>**/path/to/your/resource.txt</includes>
             </artifactItem>
           </artifactItems>
         </configuration>
       </execution>
     </executions>
    </plugin>
    
眼眸印温柔 2024-10-12 00:15:46

我认为你应该使用 JarInputStream,逐一遍历 JAR 条目,直到找到您需要的内容。然后只需 read() 找到的 JarEntry 内容即可。

I think you should use JarInputStream, traversing through JAR entries one by one, until you find what you need. Then just read() content of the found JarEntry.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文