我的可执行jar无法在Netbeans之外运行时打开资源文件 [英] My executable jar can't open resource files when I run it outside of netbeans
问题描述
在这里的第一篇文章,我希望我已经适当地提出了这个问题.我的项目在netbeans内部作为项目运行时在类路径上找到资源文件,但是当我在netbeans外部运行jar时,它找不到资源(我已经检查过,是的,它们正在放入jar中. )
first post here, I hope I've formed this question adequately. My project finds the resource files on the classpath when run inside of netbeans as a project, but when I run the jar outside of netbeans, it can't find the resources (I have checked, and yes they are being placed into the jar.)
我已经尝试了.getResource()和我的代码的.getResourceAsStream()版本.当它们作为一个Maven项目在netbeans内部运行时,它们都成功,但是当我删除jar并将其在netbeans外部运行时,找不到这些资源.
I've tried both .getResource() and also a .getResourceAsStream() version of my code. They both succeed when run inside of netbeans as a maven project, but those resources aren't being found when I remove the jar and run it outside of netbeans.
这是我的代码的摘录,也是我添加资源的pom文件中的摘录.当在netbeans中作为项目运行时,所有方法均有效,但在其他地方运行jar时,则无效.它找不到资源.
Here's a snippet of my code, and also the snippet from my pom file where I add the resources. It all works when run as a project inside netbeans, but not when I run the jar elsewhere. It fails to find the resources.
......
String wholeFile = "";
try {
BufferedReader objReader = new BufferedReader(
new java.io.InputStreamReader(
getClass().getResourceAsStream( sqlFileName )
)
);
StringBuilder sb = new StringBuilder();
String strCurrentLine;
while ( (strCurrentLine = objReader.readLine()) != null ) {
sb.append( strCurrentLine );
sb.append( System.getProperty("line.separator") );
}
wholeFile = sb.toString();
...或者最好是...
... or, preferably ...
wholeFile = java.nio.file.Files.readString(
java.nio.file.Paths.get(
getClass().getClassLoader().getResource( sqlFileName ).toURI()
)
);
// Split that file into individual create
// table strings contained in the file.
// Slit on two newlines.
String newLine = System.getProperty("line.separator");
String[] tables = wholeFile.split( newLine + newLine );
......
这是我添加到pom中的内容.但这只能在netbeans中运行才能解决.为什么?我对这里的Maven设置缺少什么?在netbeans之外...可执行jar将无法像在netbeans中运行时那样定位这些资源文件.
And here's what I added to the pom. But this only resolves if run in netbeans. Why? What am I missing about the maven setup here? Outside of netbeans ... the executable jar won't locate these resource file like they do when run inside netbeans.
先谢谢了.我希望我的问题很清楚.
Thanks in advance. I hope my question was clear.
<resources>
<resource>
<directory>src/my-resources</directory>
<includes>
<include>**/*.txt</include>
<include>**/*.jpg</include>
</includes>
</resource>
</resources>
据我所知,
推荐答案
java.nio.file.Files
无法读取* .jar文件的内容.您需要通过类加载器作为流访问资源.因此,我认为您的第一次尝试应该没问题.
java.nio.file.Files
cannot read the content of *.jar files, as far as I know. You need to access the resource via the class loader as stream. So I think your first attempt should be Ok.
可能只是路径名的问题.这是一个没有Maven的工作示例:
It's possibly just a matter of the path name. Here is a working example without Maven:
文件src/de/stefanfrings/Main.java:
File src/de/stefanfrings/Main.java:
package de.stefanfrings;
import java.io.InputStream;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
InputStream stream=Main.class.getResourceAsStream("/test.txt");
Scanner in=new Scanner(stream);
while (in.hasNextLine())
{
System.out.println(in.nextLine());
}
in.close();
}
}
文件src/de/test.txt:
File src/de/test.txt:
Hello
World
test.jar的内容:
Content of test.jar:
META-INF/MANIFEST.MF
test.txt
de/stefanfrings/Main.class
输出:
java -jar test.jar
Hello
World
如果将文件名更改为"../../test.txt",则它仅在IDE中有效,因为IDE从编译器放置其输出的(未压缩)文件夹中执行程序.
If I change the filename to "../../test.txt" then it works only in the IDE because the IDE executes the program from the (uncompressed) folder where the compiler places its output.
尝试为文件名加上"/"前缀.
Try to prefix your filename with "/".
这篇关于我的可执行jar无法在Netbeans之外运行时打开资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!