Java getResource does not find file after building artifact in Intellij -
i have used
file file = new file(game.class.getresource("tiles.txt").getfile())
to txt file resources folder , works fine when inside ide when building jar , running outside of environment throws file not found errors (which saw through running in cmd).
i use similar method images , sprite sheets:
bufferedimage loadedimage = imageio.read(game.class.getresourceasstream("eg.png"));
how differ in importing files , why path incorrect?
error cmd gives: http://imgur.com/a/1sc1l
c:\users\taka\desktop>java -jar projectc-revamped.jar java.io.filenotfoundexception: file:\c:\users\taka\desktop\projectc-revamped.jar!\tiles.txt (the filename, directory name, or volume label syntax incorrect) @ java.io.fileinputstream.open0(native method) @ java.io.fileinputstream.open(unknown source) @ java.io.fileinputstream.<init>(unknown source) @ java.util.scanner.<init>(unknown source) @ tiles.<init>(tiles.java:16) @ game.<init>(game.java:91) @ game.main(game.java:192) java.io.filenotfoundexception: file:\c:\users\taka\desktop\projectc-revamped.jar!\maps\map.txt (the filename, directory name, or volume label syntax incorrect) @ java.io.fileinputstream.open0(native method) @ java.io.fileinputstream.open(unknown source) @ java.io.fileinputstream.<init>(unknown source) @ java.util.scanner.<init>(unknown source) @ map.<init>(map.java:20) @ game.<init>(game.java:94) @ game.main(game.java:192)
url.getfile() does not convert url file. merely returns path , query portions of url. method named getfile() because url class part of java 1.0, released in 1995, when urls happened point actual files.
the path returned url.getfile() returns string may or may not valid file path. many characters may not legally appear in urls, percent-escaped. example spaces; example, file:///c:/program%20files/java
.
in short, converting url file using getfile() not safe , fail.
also, entry in .jar file not file. .jar file single archive file. entries subsequences of bytes (representing compressed data). on other hand, url returned getresource valid (assuming it’s not null), if represents .jar entry.
Comments
Post a Comment