Opoz 0 Author Share Posted May 29, 2016 Ahh i see. I know what this problem is: have had this with a PDF file i wanted to open. But my problem was that the pdf file was inside the jar file, and i fixed it using the bufferreader method. Your file is outside the jar file. Maybe it's because of admin rights, blocking the request from the jar file? Try opening the jar file in administrator mode?? My file is INSIDE the jar. The paint.png file is inside src->package->paint.png Going through an annoying process I found the ImageIO.read(URL) is returning null when it calls ImageIO.getImageReaders(ImageInputStream). Not quite sure what that means, but it sound like it thinks the image is blank. Jar files store the data in a different location, since it's all packaged into one thing. I had this issue before also, can't exactly remember what I did to fix though..... If you happen to remember I would love to know because I have spent so much time on this and am out of ideas. Link to comment Share on other sites More sharing options...
Cardozz 46 Share Posted May 29, 2016 Try this, worked for my pdf file inside the jar. if (Desktop.isDesktopSupported()) { try { File myFile = new File(("ffile.pdf")); //I dont know why, but i had to name it so that it didnt match the pdf file name, so ffile isn't a type-o xD (does make sense because the next line will search for the pdf file if it doesnt exist) if (!myFile.exists()) { // In JAR InputStream inputStream = ClassLoader.getSystemClassLoader() .getResourceAsStream("resources/file.pdf"); // Copy file OutputStream outputStream = new FileOutputStream(myFile); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length); } outputStream.close(); inputStream.close(); } Desktop.getDesktop().open(myFile); } catch (IOException ex) { // no application registered for PDFs } } I've read that files inside a JAR file are, like Mad said, located somewhere in your temp. files folder or something. By doing this, it can't read the image file from where you are requesting. On forums they said you have to create a temporary copy of the file and then read it from the copy. The above method does this exactly. It created a copy of the file and then reads it. When done, it deletes the copy file. Hope this works! let me know Edit: Change the parameter in variable myfile and inputStream (resources was a package i placed the file in, yours is just main) Link to comment Share on other sites More sharing options...
Opoz 0 Author Share Posted May 29, 2016 Try this, worked for my pdf file inside the jar. if (Desktop.isDesktopSupported()) { try { File myFile = new File(("ffile.pdf")); //I dont know why, but i had to name it so that it didnt match the pdf file name, so ffile isn't a type-o xD (does make sense because the next line will search for the pdf file if it doesnt exist) if (!myFile.exists()) { // In JAR InputStream inputStream = ClassLoader.getSystemClassLoader() .getResourceAsStream("resources/file.pdf"); // Copy file OutputStream outputStream = new FileOutputStream(myFile); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length); } outputStream.close(); inputStream.close(); } Desktop.getDesktop().open(myFile); } catch (IOException ex) { // no application registered for PDFs } } I've read that files inside a JAR file are, like Mad said, located somewhere in your temp. files folder or something. By doing this, it can't read the image file from where you are requesting. On forums they said you have to create a temporary copy of the file and then read it from the copy. The above method does this exactly. It created a copy of the file and then reads it. When done, it deletes the copy file. Hope this works! let me know Edit: Change the parameter in variable myfile and inputStream (resources was a package i placed the file in, yours is just main) Throwing a null pointer at inputStream.read(buffer) in the while loop Edit: Literally added a null check for a print statement, stopped throwing the null pointer(things like this seem to cause my code to stop throwing errors or change behavior constantly for some reason) but now it's throwing an IOException on Desktop.getDesktop().open(myFile), and if I comment out just that line then the return image is still null.' Edit 2: Adding a log statement to the while loop did nothing, so that is never even being ran. However the inputStream variable is not null. Link to comment Share on other sites More sharing options...
Cardozz 46 Share Posted May 29, 2016 Throwing a null pointer at inputStream.read(buffer) in the while loop Edit: Literally added a null check for a print statement, stopped throwing the null pointer(things like this seem to cause my code to stop throwing errors or change behavior constantly for some reason) but now it's throwing an IOException on Desktop.getDesktop().open(myFile), and if I comment out just that line then the return image is still null. Try putting the image in another package in your SRC folder. Put your main class in the main package, put the image in another package in the SRC foler, e.g. "resources". Retry . I read that the image can't be in the same folder as the class you write it in too, so it probably works if you put it in another package like i did Link to comment Share on other sites More sharing options...
Opoz 0 Author Share Posted May 29, 2016 Try putting the image in another package in your SRC folder. Put your main class in the main package, put the image in another package in the SRC foler, e.g. "resources". Retry . I read that the image can't be in the same folder as the class you write it in too, so it probably works if you put it in another package like i did So I tried and realized that the if is never actually being run. So apparently myFile exists. I have no clue what is happening anymore. Edit: I believe on that first run it created the file name I put for the temp file, but never got to deleting it because it threw an error. Edit 2: Also I have to change the ClassLoader stuff to just getClass().getResourceAsStream() or it always returns null. If I search for the image using getResource() it works, if I search using getResourceAsStream() it returns null. Link to comment Share on other sites More sharing options...
Cardozz 46 Share Posted May 29, 2016 Btw i'm heading off to bed. If the above doesnt work, maybe someone else could help. Btw i'm heading off to bed. If the above doesnt work, maybe someone else could help. Link to comment Share on other sites More sharing options...
Hopewelljnj 46 Share Posted May 29, 2016 Put a log in the catch just to see if its escaping with an error. Do log(ex.message); Link to comment Share on other sites More sharing options...
Opoz 0 Author Share Posted May 29, 2016 Put a log in the catch just to see if its escaping with an error. Do log(ex.message); I have one there. It is throwing a null pointer on the while loop because inputStream is null. I have not been able to get getResourceAsStream() to work at all while I've been trying to fix this. I added a line to getResource() as a URL, works fine, correct path, but getResourceAsStream() always returns null. Edit: getClass().getResource(fileName).openStream() is throwing an IOException but getClass().getResource(fileName) is working perfectly. For some reason the stream cannot be opened. Link to comment Share on other sites More sharing options...
Hopewelljnj 46 Share Posted May 29, 2016 Try doing while((length = inputStream.read()) > 0){ outputStream.write(length); } Link to comment Share on other sites More sharing options...
Opoz 0 Author Share Posted May 29, 2016 Try doing while((length = inputStream.read()) > 0){ outputStream.write(length); } It still throws a null pointer on inputStream.read() because it is null. It is having an issue opening the stream for the URL, therefore inputStream is null. Edit: The error is JAR entry hoWoodcutter/resources/paint.png not found in C:\Users\Jesse\DreamBot\Scripts\hoWoodcutter.jar however: Proof of location Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.