Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法读取maven jar中的资源文件_Java_Maven_Jar - Fatal编程技术网

Java 无法读取maven jar中的资源文件

Java 无法读取maven jar中的资源文件,java,maven,jar,Java,Maven,Jar,我有一个maven的小项目 项目结构如下所示: src |-main |-java |-com.my.group S3FileUploader |-resources s3.properties <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht

我有一个maven的小项目

项目结构如下所示:

src
  |-main
    |-java
      |-com.my.group
        S3FileUploader
    |-resources
      s3.properties
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.my.group</groupId>
    <artifactId>try-s3</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <version>1.11.141</version>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>*</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>
                                        com.my.group.S3FileUploader
                                    </mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
pom文件如下所示:

src
  |-main
    |-java
      |-com.my.group
        S3FileUploader
    |-resources
      s3.properties
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.my.group</groupId>
    <artifactId>try-s3</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <version>1.11.141</version>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>*</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>
                                        com.my.group.S3FileUploader
                                    </mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
但是,运行java-jar命令会出现此错误:

java.io.FileNotFoundException: file:/try-s3-1.0-SNAPSHOT-jar-with-dependencies.jar!/s3.properties (No such file or directory)
我的问题是: 1.我是不是把罐子包装错了? 2.如果没有,在代码中如何读取s3.properties文件

提前感谢:)

请尝试以下操作:

URL url = this.getClass().getResource("file.ext")
File file = new File(url.toURI());
请尝试以下操作:

URL url = this.getClass().getResource("file.ext")
File file = new File(url.toURI());

不,它正在返回NPE…不。它正在返回NPE…您不应该转换为文件,您正在尝试访问Jar中的资源,
file
用于访问本地文件系统上的文件。使用
getResourceAsStream
@markrotVeel:谢谢!!!:)您不应该转换为文件,您正在尝试访问Jar中的资源,
file
用于访问本地文件系统上的文件。使用
getResourceAsStream
@markrotVeel:谢谢!!!:)