0

I try to create a jar file with a Maven project but when i run the jar file I have a IOException.Is there something to add to the pom.xml file? I use a Maven project because i have to add jfreechart dependency.

This is the IOException :

java.io.FileNotFoundException: src\main\resources\images\NoAHMhkB6eE.png (Le che
min d?accès spécifié est introuvable)
        at java.io.RandomAccessFile.open0(Native Method)
        at java.io.RandomAccessFile.open(Unknown Source)
        at java.io.RandomAccessFile.<init>(Unknown Source)
        at javax.imageio.stream.FileImageOutputStream.<init>(Unknown Source)
        at com.sun.imageio.spi.FileImageOutputStreamSpi.createOutputStreamInstan
ce(Unknown Source)
        at javax.imageio.ImageIO.createImageOutputStream(Unknown Source)
        at javax.imageio.ImageIO.write(Unknown Source)
        at l2j1.Image.createGrayscaleImage(Image.java:69)

sample of code of the error :

public static Image createGrayscaleImage(String imgPath) throws IOException, NotAPictureException {
        String grayscaleImagePath = null ;
        BufferedImage image = null ; 
        File o = new File(imgPath);


            if (ImageIO.read(o) == null) {
                throw new NotAPictureException(o.getAbsolutePath());
            }else {
                image = (ImageIO.read(o));
            }


        BufferedImage grayscaleImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);

        for (int i = 0; i < image.getWidth() ; i++) {
            for(int j = 0; j < image.getHeight(); j++) {

                Color c = new Color(image.getRGB(i, j));

                int r = c.getRed();
                int g = c.getGreen();
                int b = c.getBlue();

                int gray = (r + g + b)/3; 

                Color gColor = new Color(gray, gray, gray); 
                grayscaleImage.setRGB(i, j, gColor.getRGB()); 

            }           
        }

        grayscaleImagePath ="src"+File.separator+"main"+File.separator+ "resources" + File.separator + "images" + File.separator + getImgName(imgPath);


            ImageIO.write(grayscaleImage, "png", new File(grayscaleImagePath));

            return new Image(grayscaleImagePath, imgPath);






    }

I try to create a InputStream like:

InputStream in = Image.class.getClassLoader().getResourceAsStream("src"+File.separator+"main"+File.separator+ "resources" + File.separator + "images" + File.separator + getImgName(imgPath));


    grayscaleImagePath ="src"+File.separator+"main"+File.separator+ "resources" + File.separator + "images" + File.separator + getImgName(imgPath);


    byte[] buffer = new byte[in.available()];
    in.read(buffer);
    File targetFile = new File("src/main/resources"+File.separator + getImgName(imgPath));
    OutputStream outStream = new FileOutputStream(targetFile);
    outStream.write(buffer);
    outStream.close();
        ImageIO.write(grayscaleImage, "png", targetFile);

        return new Image(grayscaleImagePath, imgPath);

but i have a NullPointerException on buffer array.

Thanks you

Axel
  • 15
  • 5
  • 2
    Please provide whole exception. I think that your jar tries to open a file, but it has wrong specified path – MrHolal Apr 23 '19 at 19:20
  • Ok, but when i try the program on Eclipse directly there is no IOExecption.But it is only when i run the program with jar file that this is this Exception. – Axel Apr 23 '19 at 20:01
  • So I can't provide you the Exception on the Eclipse console because there is no Exception when I run with Eclipse – Axel Apr 23 '19 at 20:02
  • Of course you can provide exception.. just start the jar from command line.. if you are using windows, open cmd and `java -jar yourApp.jar` – MrHolal Apr 23 '19 at 20:06
  • I created the file like this Eclipse->File->Export->Jar Executable->Finish. – Axel Apr 23 '19 at 20:09
  • So start the exported file with command i wrote here – MrHolal Apr 23 '19 at 20:11
  • Ok thank, my app is a windows with severals buttons, i have to click on a button to have an Exception. – Axel Apr 23 '19 at 20:13
  • is there a solution to click on a button with a command? – Axel Apr 23 '19 at 20:14
  • When you click the button, exception should be in command line – MrHolal Apr 23 '19 at 20:15
  • Ok thank there is a FileNotFoundException – Axel Apr 23 '19 at 20:21
  • can i send you the exception? – Axel Apr 23 '19 at 20:22
  • Edit your original question and add it there – MrHolal Apr 23 '19 at 20:25
  • When i change the project to Maven project i had to change the path of output file that the program create – Axel Apr 23 '19 at 20:30
  • Im not sure if i understand what you mean... please provide sample of code where you are uploading ` src\main\resources\images\NoAHMhkB6eE.png` to app – MrHolal Apr 23 '19 at 20:34
  • ok the line is : grayscaleImagePath ="src"+File.separator+"main"+File.separator+ "resources" + File.separator + "images" + File.separator + getImgName(imgPath); – Axel Apr 23 '19 at 20:41
  • I created a new directory source "picture" then i changed the path on the code, there is no Exception on eclipse but with the File jar always the same error but now picture\NoAHMhkB6eE.png – Axel Apr 23 '19 at 21:05
  • try to change grayscaleImagePath to `getClass().getResourceAsStream("/com/mycompany/myapplication/src/main/resources/yourimg"));` – MrHolal Apr 24 '19 at 05:44
  • maybe i have to create a bufferedReader? because the output of getClass().getResourceAsStream("/com/mycompany/myapplication/src/main/resources/yourimg")); is InputStream and i have a string – Axel Apr 24 '19 at 08:31
  • when i do a inputStream this is null : InputStream in = getClass().getResourceAsStream("/com/mycompany/myapplication/src/main/resources/yourimg.png"); if(in == null) { System.out.println("null"); } – Axel Apr 24 '19 at 09:14
  • If its complaining about file not exists, it not having any sense to try to implement another logic to open it, basically, the path seems to be not correct. As far as I can remember, `File` has something like `exists()` method and ` `getAbsolutePath()`, so you can use that approach for testing - try to print out the path and then refer to your machine, if the file is really on this path or not. You can create `File` with nonexisting content, but then once its used, it start to fail- eg. `ImageIO.read` – xxxvodnikxxx Apr 24 '19 at 09:41
  • Its complaining about file not exists, because jar doesnt behave same as eclipse.. you need to specify the path correctly, not `/com/mycompany/myapplication/src/main/resources/yourimg.png` - this is just example, you need to insert you path – MrHolal Apr 24 '19 at 09:44

1 Answers1

0

According to the code you posted, you are trying to save your image file under this folder...

src\main\resources\images\

This is a relative path. It is relative to the value of java System property user.dir. It seems that the value of this property is different when you run your code in Eclipse than when you run your JAR.

Either use an absolute path or make sure that user.dir points to the same location when you run from Eclipse and from your JAR.

Of-course, if the actual location of the saved file is not important, you can also use the value of System property "java.io.tmpdir" or "user.home" which do not change.

Abra
  • 19,142
  • 7
  • 29
  • 41
  • i saw that https://stackoverflow.com/questions/16239130/java-user-dir-property-what-exactly-does-it-mean/16239152 just to be sure, if i put my program on C:/my_program/ and i edit the absolute path in the code like C:/my_program/picture.png is that correct? – Axel Apr 24 '19 at 11:51
  • Thanks you the user.dir was not the same as you said I just had to move my programm to user.dir path of Eclipse and then run to have the same path – Axel Apr 24 '19 at 13:08