3

I just posted this question: How to distribute my Java program so that it is runnable by double-clicking a single file? and while I got the runnable jar working, many things don't work the way I'd want them to since my program cannot find the resources I want to use such as images and a SQLite database file.

In my file system, I have several .png images in the img folder and a database located at the project's root folder named test.db

While my project was in Eclipse, I accessed my images using something like:

BufferedImage bufImg = ImageIO.read(new File("img/anImage.png"))

And I connected to my SQLite database using:

Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:test.db");

Now, when I double-click my newly created runnable-jar, the resources cannot be found. I'd like to know how I'm supposed to access them and have my program work when it is not in a IDE.

I would also like to know how I can see what the exceptions are (if at all possible). Because right now, some pages stay gray because they don't load because (I'm guessing) some resources cannot be found, but I have no idea if it's only the database that is causing a problem or if it's also the images.

Thanks a ton and sorry for asking two related questions in such a small timeframe!

Community
  • 1
  • 1
Jumbala
  • 4,764
  • 9
  • 45
  • 65
  • *"sorry for asking two related questions in such a small timeframe!"* They were both good questions so I cannot accept your apology - I'll just up-vote. – Andrew Thompson Aug 22 '11 at 04:36

1 Answers1

5

There are no Files within a Jar, only resources, so most methods that use files will also accept resources.

For instance ImageIO.read(...) has an overload that accepts URL and one that accepts InputStream. So try

BufferedImage bufImg = ImageIO.read(MyClass.class.
         getResourceAsStream("img/anImage.png"));

Just be sure to use a path relative to the class file location and your actual class name, or this.getClass()

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I see, I'm going to try that and see how it works. Just a question, though: will the resource methods work even when my application isn't in a jar? If they do, I fail to see why one should even use the File version since it's going to break when you put it into a jar? Also, how can I access files that have no methods that can access them as resources? – Jumbala Aug 22 '11 at 02:17
  • Yes, the resource version should work with non-jar'd classes. The main limitation of resources is that the are *read-only*, and files don't have this limitation. Regarding accessing files without methods that allow for resources -- I can't answer that general a question. Do have a specific example in mind? – Hovercraft Full Of Eels Aug 22 '11 at 02:33
  • I don't, I just didn't want to get stuck if I ever came across one in the future... With your last comment "The main limitation of resources is that the are read-only", there's something I don't fully understand though... If the resources are read-only, does that mean that I won't be able to put my SQLite database file in a JAR file since I'm going to want to write some new rows in it when I get new data in my application? Would I be better off using an absolute path outside the jar to access the file? – Jumbala Aug 22 '11 at 02:47
  • Yes, that is absolutely correct, you cannot have anything that needs to be modified within the jar. So keep it outside the jar, and pass the path to the database on the command line when calling the jar. I've done this in Windows by creating a windows shortcut or bat file. – Hovercraft Full Of Eels Aug 22 '11 at 02:53
  • My crystal ball was telling me that this DB was read **&** write. The numerology merely indicated it was a focused DB with a well defined aura. ;) – Andrew Thompson Aug 22 '11 at 03:08
  • My aura sense was obviously way off tonight, and so was my ability to read the animal entrails. I've deleted the junk I wrote about accessing the database in the jar in my answer above. – Hovercraft Full Of Eels Aug 22 '11 at 03:11
  • Thanks again for the help, I really appreciate it! I'm testing it right now and it works great, thanks! – Jumbala Aug 22 '11 at 03:36
  • Wonderful! Much luck on your project! – Hovercraft Full Of Eels Aug 22 '11 at 03:38
  • 1
    *"..so was my ability to read the animal entrails."* Don't eschew the haruspictory examinations yet. Learn to read the entrails - and all will become known (and somewhat slippery)! – Andrew Thompson Aug 22 '11 at 04:19