fatjar打包config路径问题

按照Jfinaldemo的方式配置fatjar方式打包后,无法读取resource文件夹下面的配置文件。

main方法中打印的rootclassPath值为     D:/print/newFolder/fukuwaraku-print.jar  image.png

在onstart方法中打印的rootclassPath值为 D:/print/newFolder/config,这个config是怎么来的。为什么不从jar包中读取配置文件

image.png

我的主要目的是读取jar包中的report模板文件跟fonts下面的字体文件。

报错如下:java.io.FileNotFoundException: D:\print\newFolder\config\report\WMS_SAGAWA_BLUE.jasper

image.png

POM文件

<build>
        <finalName>fukuwarakuwms</finalName>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <!-- java8 保留参数名编译参数 -->
                    <!-- compilerArgument>-parameters</compilerArgument -->
                    <compilerArguments>
                        <extdirs>src\main\webapp\WEB-INF\lib</extdirs>
                        <!--该死的压缩图片,删除原来功能,换成阿里的包压缩-->
                        <!--<bootclasspath>${java.home}\jre\lib\rt.jar;${java.home}\jre\lib\jce.jar</bootclasspath>-->
                    </compilerArguments>
                </configuration>
            </plugin>

            <!-- 安装源码到本地仓库 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.1.2</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>8.1.8.v20121106</version>
                <configuration>
                    <stopKey>stop</stopKey>
                    <stopPort>5599</stopPort>
                    <webAppConfig>
                        <contextPath>/</contextPath>
                    </webAppConfig>
                    <scanIntervalSeconds>5</scanIntervalSeconds>
                    <connectors>
                        <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                            <port>80</port>
                            <maxIdleTime>60000</maxIdleTime>
                        </connector>
                    </connectors>
                </configuration>
            </plugin -->
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.jf</include>
                    <include>**/*.jasper</include>
                </includes>
                <!-- 是否替换资源中的属性-->
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
            <resource>
                <directory>src/main/webapp</directory>
                <includes>
                    <include>**/*.xls</include>
                    <include>**/*.xlsx</include>
                    <include>**/*.jrxml</include>
                    <include>**/*.jasper</include>
                </includes>
            </resource>
        </resources>
    </build>


评论区

JFinal

2020-11-18 15:38

在onstart方法中打印的rootclassPath值为 D:/print/newFolder/config

是 jfinal undertow 约定的存放配置文件的地方,它也是 class path 的一部分

java 规范里头, class path 可以有多个,也可以指向 jar 文件。 你在打成 fatjar 以后, class path 就两个,一个是那个 config 目录,另一个就是你打成的这个 jar 包

读取这两处的配置文件,可以使用 Prop 与 PropKit, 没有问题的

hb963724769

2020-11-18 15:55

@JFinal 那当jar相同目录下没有config这个文件夹时,我的resource文件应该从哪里读取,现在我的jar包里面有这些resource但是运行时无法读取,报错java.io.FileNotFoundException: D:\print\newFolder\config\report\WMS_SAGAWA_BLUE.jasper