开发者

Locating resources within a jar package

开发者 https://www.devze.com 2023-01-19 12:39 出处:网络
I have a static class and inside that class images are loaded into BufferedImage objects like so: File groundTopImageFile = new File(\"src/main/resources/ground - grass top.png\");

I have a static class and inside that class images are loaded into BufferedImage objects like so:

File groundTopImageFile = new File("src/main/resources/ground - grass top.png");

Now when I create an executable jar out of this using Maven2 everything works, except it doesn't find the image files. I checked the jar, and the image files have all been put in the root of the jar, so I tried using:

File groundTopImageFile = new File("ground - grass top.png");

but no success. Also in this way I can't use the same code within Eclipse anymore. Is there some way to make this work both in the jar and in Eclipse?

This is my Pom.xml file:

<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>
          开发者_Go百科  <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>
            </manifest>
            <manifestEntries>
              <mode>under development</mode>
              <url>www.WetWindmill.com/Projects/Sheepness/</url>
            </manifestEntries>
          </archive>
        </configuration>
      </plugin>
    </plugins>

    <resources>
      <resource>
        <directory>${basedir}/target/dependency</directory>
      </resource>
      <resource>
        <directory>${basedir}/src/main/resources</directory>
      </resource>
    </resources>
  </build>
</project>


Use ImageIO.read(inputStream) and read the InputStream using ClassLoader.getResourceAsStream().

Sample Code:

String resourceName = "my/picfolder/mypic.jpg";
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream stream = classLoader.getResourceAsStream(resourceName);
BufferedImage bi = ImageIO.read(stream);

Note:

In my code example, my/picfolder is the relative folder hierarchy below src/main/resources.


URL imgURL = getClass().getResource("/ground - grass top.png");
if (imgURL != null)
{
  return new ImageIcon(imgURL, "Ground Grass Top");
}
else
{
  logger.error("Couldn't find file: " + "ground - grass top.png");
  return null;
}


Use the class loader getResourceAsStream() method.


Instead of using "/./resources/back_img.png", use "resources/back_img.png" with ClassLoader. Here is example :

    String path = "resources/back_img.png";
    ClassLoader cl = ImageHandler.class.getClassLoader();
    URL imgURL = cl.getResource(path);
    //URL imgURL = ImageHandler.class.getResource(path);

    if (imgURL != null) {
        ImageIcon icon = new ImageIcon(imgURL, description);
        Image img = icon.getImage();
        Image sizedImg = img.getScaledInstance(width, height, Image.SCALE_DEFAULT);
        return new ImageIcon(sizedImg);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }


You solves my problem,Sean Patrick Floyd. Thank It's seem different between IDEs but, see structure of jar file, we know what relative path. As my case, I used Netbean, my file put in src/my/picfolder/mypic.jpg (It 's src/main/resources/my/picfolder/mypic.jpg with Sean), but when built, extract jar file we 'll see image in my/picfolder/mypic.jpg . So relative path must be my/picfolder/mypic.jpg when we call getResourceAsStream

0

精彩评论

暂无评论...
验证码 换一张
取 消