小编mdp*_*mdp的帖子

从jar访问jar外部的资源

我正在尝试从jar文件中访问资源.资源位于jar的同一目录中.

my-dir:
 tester.jar
 test.jpg
Run Code Online (Sandbox Code Playgroud)

我尝试了不同的东西,包括以下内容,但每次输入流为空时:

[1]

String path = new File(".").getAbsolutePath();
InputStream inputStream = this.getClass().getResourceAsStream(path.replace("\\.", "\\") + "test.jpg");
Run Code Online (Sandbox Code Playgroud)

[2]

File f = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
InputStream inputStream = this.getClass().getResourceAsStream(f.getParent() + "test.jpg");
Run Code Online (Sandbox Code Playgroud)

你能给我一些提示吗?谢谢.

java inputstream getresource

9
推荐指数
1
解决办法
7026
查看次数

标签 统计

getresource ×1

inputstream ×1

java ×1