运行jar包时提示:java.io.FileNotFoundException: nullsampledata/movies.csv
FrankWork opened this issue · 4 comments
$ java -jar SparrowRecSys-1.0-SNAPSHOT-jar-with-dependencies.jar
log4j:WARN No appenders could be found for logger (org.eclipse.jetty.util.log).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
webRootLocation.toURI().toASCIIString(): jar:file:/mnt/e/work/SparrowRecSys/target/SparrowRecSys-1.0-SNAPSHOT-jar-with-dependencies.jar!/webroot/index.html
Web Root URI: null
Loading movie data from nullsampledata/movies.csv ...
Exception in thread "main" java.io.FileNotFoundException: nullsampledata/movies.csv (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
at java.base/java.io.FileInputStream.(FileInputStream.java:157)
at java.base/java.util.Scanner.(Scanner.java:639)
at com.sparrowrecsys.online.datamanager.DataManager.loadMovieData(DataManager.java:56)
at com.sparrowrecsys.online.datamanager.DataManager.loadData(DataManager.java:41)
at com.sparrowrecsys.online.RecSysServer.run(RecSysServer.java:54)
at com.sparrowrecsys.online.RecSysServer.main(RecSysServer.java:21)
java -jar SparrowRecSys-1.0-SNAPSHOT-jar-with-dependencies.jar
log4j:WARN No appenders could be found for logger (org.eclipse.jetty.util.log).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
webRootLocation: jar:file:/mnt/e/work/SparrowRecSys/target/SparrowRecSys-1.0-SNAPSHOT-jar-with-dependencies.jar!/webroot/
Web Root URI: jar:file:/mnt/e/work/SparrowRecSys/target/SparrowRecSys-1.0-SNAPSHOT-jar-with-dependencies.jar!/webroot/
Loading movie data from jar:file:/mnt/e/work/SparrowRecSys/target/SparrowRecSys-1.0-SNAPSHOT-jar-with-dependencies.jar!/webroot/sampledata/movies.csv ...
Exception in thread "main" java.io.FileNotFoundException: jar:file:/mnt/e/work/SparrowRecSys/target/SparrowRecSys-1.0-SNAPSHOT-jar-with-dependencies.jar!/webroot/sampledata/movies.csv (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
at java.base/java.io.FileInputStream.(FileInputStream.java:157)
at java.base/java.util.Scanner.(Scanner.java:639)
at com.sparrowrecsys.online.datamanager.DataManager.loadMovieData(DataManager.java:56)
at com.sparrowrecsys.online.datamanager.DataManager.loadData(DataManager.java:41)
at com.sparrowrecsys.online.RecSysServer.run(RecSysServer.java:58)
at com.sparrowrecsys.online.RecSysServer.main(RecSysServer.java:21)
通过修改:SparrowRecSys-master/src/main/java/com/sparrowrecsys/online/RecSysServer.java文件里面:
设置为相应文件的绝对路径可以临时解决:
String path = "/Users/github/SparrowRecSys-master/src/main/resources/webroot/";
DataManager.getInstance().loadData(path + "sampledata/movies.csv" .........
具体原因还得等大佬来回答
问题在于本身这个URI指向的是这个jar包,他是个文件,然后底下的load方法尝试使用Scanner去读取文件(jar包)中的文件,这是有问题的,需要改成使用Stream的方式去读取
loadMovieData等方法中修改Scanner读取的方式
Scanner scanner = new Scanner(new BufferedInputStream(this.getClass().getResourceAsStream(movieDataPath)));
然后启动类中修改为相对路径即可
DataManager.getInstance().loadData("/webroot/sampledata/movies.csv",
"/webroot/sampledata/links.csv","/webroot/sampledata/ratings.csv",
"/webroot/modeldata/item2vecEmb.csv",
"/webroot/modeldata/userEmb.csv",
"i2vEmb", "uEmb");
如果了解一点Spring Boot 不妨看看Java的实现:https://github.com/iqiancheng/sparrow-recsys-spring-boot
欢迎探讨