相关疑难解决方法(0)

在jar包中定位资源

我有一个静态类,在类中,图像被加载到BufferedImage对象中,如下所示:

File groundTopImageFile = new File("src/main/resources/ground - grass top.png");
Run Code Online (Sandbox Code Playgroud)

现在,当我使用Maven2创建一个可执行jar时,一切正常,除了它找不到图像文件.我检查了jar,图像文件都放在了jar的根目录中,所以我尝试使用:

File groundTopImageFile = new File("ground - grass top.png");
Run Code Online (Sandbox Code Playgroud)

但没有成功.同样,我不能再在Eclipse中使用相同的代码了.有没有办法在jar和Eclipse中使这个工作?

这是我的Pom.xml文件:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.WetWindmill</groupId>
  <artifactId>Sheepness</artifactId>
  <name>Sheepness</name>
  <version>0.0.1-SNAPSHOT</version>
  <description>Equilibrium reaction visualized with sheep :)</description>

  <dependencies>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.4</version>
    <scope>test</scope>
  </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.5.8</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>

      <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>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <mainClass>controller.Sheepness</mainClass>
              <packageName>com.WetWindmill.Sheepness</packageName> …
Run Code Online (Sandbox Code Playgroud)

java resources jar

4
推荐指数
1
解决办法
6211
查看次数

标签 统计

jar ×1

java ×1

resources ×1