Cannot find a resource file, after exporting java project as JAR, and trying to use it in a Dynamic Web Project in ECLIPSE

让人想犯罪 __ 提交于 2019-12-08 09:48:36

问题


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/


回答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.



来源:https://stackoverflow.com/questions/50571771/cannot-find-a-resource-file-after-exporting-java-project-as-jar-and-trying-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!