11
public static void imRes(String pat) {
        try {
            BufferedImage bckimg = ImageIO.read(new File("c:/s/deneme.jpg"));
            File s = new File(pat);
            BufferedImage im = ImageIO.read(s);
            BufferedImage im1 = resIm(im);
            BufferedImage finIm = mergIm(im1, bckimg);
            ImageIO.write(finIm, "jpg", new File("c:/s/deneme1.jpg"));
        } catch (IOException e) {

            e.printStackTrace();
        }

This is my first post, excuse me if I've done something wrong. This code was running properly untill i try to read an image from the source package. But now it can't read any image. What am I doing wrong? Or is it something about eclipse?

Exception:

javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(Unknown Source)
    at imRe.imRes(imRe.java:12)
    at imReTest.main(imReTest.java:6)

Thanks...

s.alem
  • 12,579
  • 9
  • 44
  • 72
  • 2
    Welcome to StackOverflow! Generally when posting a question about a particular problem like this, it's helpful to provide information about the actual error your receiving. Are you getting an exception in your `catch` block? Is one of the methods returning `null`? By providing more information, we can help with your exact problem instead of guessing. Good question, btw, +1 – Brian Oct 29 '12 at 23:16
  • What do you mean by "source package"? What is your Exception. – Tinman Oct 29 '12 at 23:17
  • It reads from "deneme.jpg" and writes to "deneme1.jpg". This is just a demo, and the point of the demo is merging two images. – s.alem Oct 29 '12 at 23:57
  • 1
    It sounds like the expected location for the image files either no longer exists or the files to not exist within in. Try adding `System.out.println(new File("c:/s/deneme.jpg").exists())` before you try to load the image to see if the files are actually reachable – MadProgrammer Oct 30 '12 at 00:08

1 Answers1

15

Change / for \ if you are using windows.

A more cross-platform approach would be substitute

C: for File.listRoots()[0] and every / for File.separator.

Read more on the File api documentation

EDIT

(I didn't read this line, sorry)

This code was running properly untill i try to read an image from the source package

In order to get a file from inside your jar package, one must use the getClass().getResource() method.

Example:

application-package:
|-Main.java
|-resources
  |-image.jpg

For the above directory structure:

BufferedImage im = ImageIO.read(new File(getClass().getResource("/resources/image.jpg").toURI()));

Would do the trick.

Bruno Vieira
  • 3,884
  • 1
  • 23
  • 35
  • AFAIK, Java doesn't care about this. See [this question](http://stackoverflow.com/questions/2417485/file-separator-vs-slash-in-paths). If anything this would _break_ the program for other platforms. – Brian Oct 29 '12 at 23:12
  • 2
    `File.separator`, not `File.pathSeparator`. The path separator is used to separate individual files or directories, such as when specifying the classpath or the `%PATH%` variable on Windows (`$PATH` on UNIX). – Brian Oct 29 '12 at 23:16
  • I edited in attempt to correct my previous mistakes. Thanks for the comments – Bruno Vieira Oct 29 '12 at 23:21
  • Thanks for getResource code, but it says "cannot convert File to BufferedImage". Am I doing something wrond? – s.alem Oct 29 '12 at 23:37
  • Well, you copy pasted my code, I was trying to make an example for it, I'll make it copy and pastable on my next edition – Bruno Vieira Oct 29 '12 at 23:38
  • Oh, my mistake, sorry. There's a "static" problem, but it's because of my code. I guess i should change the structure. But now I know getClass().getResource() method. Thanks a lot... – s.alem Oct 29 '12 at 23:53