0

I have got this class for loading blue images, which works fine in Eclipse but not in the exported jar. How can I access all the blue images in the folder (directory) called "blue" without knowing the names of the images?

public class Blue
{
   public static void read() throws Exception
   {
      File directoryBlueImages = new File(
            Blue.class.getResource("blue").getFile());
      String[] blueImages = directoryBlueImages.list();
      List<BufferedImage> blueImagesList = new ArrayList<>();
      for (String blueImage : java.util.Objects.requireNonNull(blueImages))
      {
         blueImagesList.add(ImageIO
               .read(Blue.class.getResourceAsStream("blue/" + blueImage)));
      }
      ApplicationImages.setBlueImages(blueImagesList);
   }
}

UPDATE

I have tried this, but it does not work either. I am getting a NullPointer exception. I tried "/blue" and "blue" and even ".blue".

import java.awt.image.BufferedImage;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import javax.imageio.ImageIO;

import vokabeltrainer.ApplicationImages;

public class Blue
{
   public static void read() throws Exception
   {
      List<BufferedImage> blueImagesList = new ArrayList<>();

      try (Stream<Path> pathStream = Files.walk(Paths.get(Blue.class
            .getClassLoader().getResource("blue").toURI().toURL().getPath()))
            .filter(Files::isRegularFile))
      {
         for (Path file : (Iterable<Path>) pathStream::iterator)
         {
            blueImagesList.add(ImageIO
                  .read(Blue.class.getResourceAsStream(file.toString())));
            ;
         }
      }

      ApplicationImages.setBlueImages(blueImagesList);
   }
}

1 Answers1

0

I adapted an answer from How to list the files inside a JAR file?

First I distinguish wether I am running from jar or Eclipse:

  try
  {
     Blue.readZip(); // when inside jar
  }
  catch (Exception e)
  {
     try
     {
        Blue.read(); // during development
     }
     catch (Exception e1)
     {
        System.out.println("Could not read blue.");
        e1.printStackTrace();
     }
  }

Then class Blue looks like this:

public class Blue
{
   private static List<BufferedImage> blueImagesList = new ArrayList<>();

   public static void read() throws Exception
   {
      File directoryBlueImages = new File(
            Blue.class.getResource("blue").getFile());
      String[] blueImages = directoryBlueImages.list();

      for (String blueImage : java.util.Objects.requireNonNull(blueImages))
      {

         blueImagesList.add(ImageIO
               .read(Blue.class.getResourceAsStream("blue/" + blueImage)));

      }
      ApplicationImages.setBlueImages(blueImagesList);
   }

   public static void readZip() throws Exception
   {
      CodeSource src = Blue.class.getProtectionDomain().getCodeSource();
      if (src != null)
      {
         URL jar = src.getLocation();
         ZipFile zipFile = new ZipFile(jar.getFile());
         ZipInputStream zip = new ZipInputStream(jar.openStream());
         while (true)
         {
            ZipEntry ze = zip.getNextEntry();
            if (ze == null)
               break;
            String name = ze.getName();
            if (name.startsWith("vokabeltrainer/resources/blue/"))
            {
               blueImagesList.add(ImageIO.read(zipFile.getInputStream(ze)));
            }
         }
      }
      else
      {
         throw new IOException("can not find code source for blue images");
      }
      ApplicationImages.setBlueImages(blueImagesList);
   }
}