简体   繁体   English

将Java项目导出为JAR,并尝试在ECLIPSE的动态Web项目中使用它后,找不到资源文件

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

I have a working java program, which is doing some database work with derby. 我有一个工作的Java程序,它正在使用derby进行一些数据库工作。 the DB url for connection 用于连接的数据库URL

jdbc:derby://localhost:1527/MaorB_CouponDB JDBC:德比://本地主机:1527 / MaorB_CouponDB

is not hard coded but instead is written in a .txt file, located in a 'files' folder, not the src. 不是硬编码 ,而是写入.txt文件(位于“文件”文件夹中,而不是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. 该项目的第二阶段正在全部转移到Web上,该java程序充当服务器上执行DB动作的大脑。 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). 我将整个项目导出为.JAR,并且可以看到它还包含“文件”文件夹(带有url txt和其他文件)。

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: 但是,尝试运行一个简单的测试以在新的Dynamic Web Project上进行检查(在将project.jar,derby_client.jar作为外部库导入之后)会导致异常:

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. 手动将.txt文件复制到新的Web项目中的多个位置不会产生任何结果。

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. 但是在尝试使用路径字符串进行很多变体后,我只会得到null。

any suggestions? 有什么建议么? thanks. 谢谢。


EDIT: here's everything I tried and I think is relevant: resulting in null while trying to get resource 编辑:这是我尝试过的所有事情,我认为是相关的: 尝试获取资源时导致null


EDIT2: inputstream is null, txt is under src/newfiles/ EDIT2: inputstream为null,txt在src / newfiles /下

Make sure the resource file is in the result jar. 确保资源文件在结果jar中。 This is often the cause of such errors. 这通常是此类错误的原因。 If you post your build file, it will be easier to help. 如果发布构建文件,将更容易获得帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM