0

I have a working java program, which is doing some database work with derby. the DB url for connection

jdbc:derby://localhost:1527/MaorB_CouponDB

is not hard coded but instead is written in a .txt file, located in a 'files' folder, not the src. running the project and testing the system like this works perfect.

the 2nd phase of the project is moving all to web, with this java program acting as the brain on the server to perform DB actions. I exported the whole project as a .JAR and I can see it also included the 'files' folder (with the url txt, along other files).

however, trying to run even a simple test to check it on the new Dynamic Web Project (after importing the project.jar, derby_client.jar as external libs)results in an exception:

ERROR: something went wrong. cannot initialize a ConnectionPool instance. cause:
URL file not found, on connection pool CTOR
x.exceptions.ConnectionPoolException: URL file not found, on connection pool CTOR
    at b.connections.ConnectionPool.<init>(ConnectionPool.java:34)
    at b.connections.ConnectionPool.getInstance(ConnectionPool.java:73)
    at d.DAO.CompanyDbDAO.<init>(CompanyDbDAO.java:26)
    at f.system.CouponSystem.<init>(CouponSystem.java:36)
    at f.system.CouponSystem.getInstance(CouponSystem.java:58)
    at a.test.main(test.java:11)
Caused by: java.io.FileNotFoundException: files\urlFile (The system cannot find the path specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.util.Scanner.<init>(Unknown Source)
    at b.connections.ConnectionPool.<init>(ConnectionPool.java:31)
    ... 5 more

manually copying the .txt file to several places in the new web project yields no results.

a quick research here and in other places suggests somehow using :

this.getClass().getResourceAsStream("/files/urlFile.txt");

but after trying quite a few variations with the path string, I get only null.

any suggestions? thanks.


EDIT: here's everything I tried and I think is relevant: resulting in null while trying to get resource


EDIT2: inputstream is null, txt is under src/newfiles/

Maor Barazani
  • 680
  • 3
  • 12
  • 31
  • Possible duplicate of [Reading a resource file from within jar](https://stackoverflow.com/questions/20389255/reading-a-resource-file-from-within-jar). Just a) create a "files" directory *UNDER "src" in your Eclipse project*, b) copy urlfile.txt into it, and c) rebuild your .jar. `getResourceAsStream()` should now work as expected. – paulsm4 May 28 '18 at 18:27
  • this results in null: InputStream urlFile = this.getClass().getResourceAsStream("src/newfiles/urlFile.txt"); System.out.println(urlFile); – Maor Barazani May 29 '18 at 07:35
  • NO! NO! NO! Don't reference "src" in your code! Eclipse automatically copies stuff from non-Java resources (like "src/files/*") that are *UNDER* "src" into your jar. Do this: a) Create "files" under "src". b) Copy "url.txt" into "src/files". c) Modify your code: `...getResourceAsStream("/files/url.txt")`, d) Rebuild your .jar and test. – paulsm4 May 29 '18 at 16:17
  • First of all, I appreciate your trying to help, really. I'm kinda lost in here and cannot move forward with the project until I can sort this out. Still in the main, 'java' only original project- when i try what you suggested, it results in 'null'. from which I obviously cannot get the url String. am I missing something? ** I edited the original post with another screenshot- a simple one. – Maor Barazani May 29 '18 at 19:17

1 Answers1

1

Make sure the resource file is in the result jar. This is often the cause of such errors. If you post your build file, it will be easier to help.

Guilherme Mussi
  • 956
  • 7
  • 14