好的,所以在IDE中它运行得很好并且说该文件存在,但是一旦我将它打包到jar中这段代码:
File levelFolder = new File(getClass().getResource("data/level").getPath());
System.out.println("exists = " + levelFolder.exists());
表示该文件夹不存在。知道为什么吗?
编辑:
jar文件列表。
G:\java>jar tvf phantasyblade.jar
6574 Wed Nov 10 22:36:34 PST 2010 META-INF/MANIFEST.MF
6334 Wed Nov 10 22:36:34 PST 2010 META-INF/CYPRI.SF
992 Wed Nov 10 22:36:34 PST 2010 META-INF/CYPRI.RSA
0 Wed Nov 10 22:31:32 PST 2010 META-INF/
717 Mon Nov 08 00:08:30 PST 2010 Switch.class
935 Wed Nov 10 22:21:14 PST 2010 Sword1.class
803 Tue Nov 09 13:42:38 PST 2010 SwordBlock.class
2674 Tue Nov 09 13:42:38 PST 2010 Thing.class
0 Wed Nov 10 22:32:56 PST 2010 data/
0 Wed Nov 10 22:32:56 PST 2010 data/audio/
108669 Mon Nov 30 22:32:02 PST 2009 data/audio/boss1.ogg
30866 Wed Nov 25 13:40:12 PST 2009 data/audio/edie.wav
11440 Wed Dec 02 15:22:00 PST 2009 data/audio/ehit.wav
191532 Fri Apr 16 07:32:46 PDT 2010 data/audio/itemget.wav
154856 Mon Nov 30 21:55:18 PST 2009 data/audio/map1.ogg
18572 Wed Aug 19 02:16:30 PDT 2009 data/audio/pdie.wav
30924 Wed Dec 02 15:02:54 PST 2009 data/audio/pget.wav
10044 Wed Aug 19 02:13:52 PDT 2009 data/audio/phit.wav
24920 Wed Dec 02 15:08:44 PST 2009 data/audio/plife.wav
17399 Mon Aug 24 17:40:00 PDT 2009 data/audio/sword.wav
71946 Mon Nov 30 22:36:08 PST 2009 data/audio/title.ogg
0 Wed Nov 10 22:32:56 PST 2010 data/graphics/
5486 Mon Dec 07 23:15:46 PST 2009 data/graphics/alienzaku1.png
1372 Mon Dec 14 02:05:44 PST 2009 data/graphics/ammo15.png
888 Wed Nov 10 20:07:38 PST 2010 data/graphics/block.png
1841 Mon Dec 07 16:50:00 PST 2009 data/graphics/bluegem.png
101871 Mon Dec 07 22:59:30 PST 2009 data/graphics/boss1.png
9206 Mon Dec 14 13:21:26 PST 2009 data/graphics/bossblock1.png
280 Tue Nov 18 08:13:44 PST 2008 data/graphics/bullet.png
2831 Wed Dec 16 14:11:30 PST 2009 data/graphics/chargeshot.png
277 Tue Nov 18 16:12:32 PST 2008 data/graphics/ebullet.png
1928 Wed Dec 16 14:05:14 PST 2009 data/graphics/echargeshot.png
3573 Wed Dec 02 15:07:06 PST 2009 data/graphics/ehealth.png
6158 Mon Dec 14 22:17:06 PST 2009 data/graphics/gameover.png
1594 Mon Dec 07 16:49:32 PST 2009 data/graphics/greengem.png
5279 Mon Dec 07 16:49:44 PST 2009 data/graphics/health.png
5591 Wed Dec 16 22:37:20 PST 2009 data/graphics/hexcontainer.png
23037 Tue Dec 15 13:03:32 PST 2009 data/graphics/hud.png
8173 Thu Dec 03 00:22:14 PST 2009 data/graphics/hud1.png
280 Tue Nov 18 08:13:44 PST 2008 data/graphics/hudbullet.png
606 Wed Dec 02 04:58:50 PST 2009 data/graphics/hudgem.png
3527 Wed Dec 02 15:07:02 PST 2009 data/graphics/hudhealth.png
13726 Wed Dec 16 14:40:36 PST 2009 data/graphics/hunter.png
666 Wed Nov 10 21:09:54 PST 2010 data/graphics/key.png
11010 Thu Nov 26 11:14:02 PST 2009 data/graphics/levelclear.png
1306 Wed Nov 10 20:08:42 PST 2010 data/graphics/lockedblock.png
2340 Mon Dec 14 16:32:26 PST 2009 data/graphics/mapsword.png
1448 Wed Nov 10 20:20:58 PST 2010 data/graphics/moveblock.png
1660 Sun Nov 07 12:54:42 PST 2010 data/graphics/neblock.png
30258 Mon Dec 07 16:50:28 PST 2009 data/graphics/neoscout1.png
9569 Mon Dec 07 23:25:08 PST 2009 data/graphics/player.png
5903 Mon Dec 07 16:36:02 PST 2009 data/graphics/playerdead.png
3910 Fri Apr 16 07:23:12 PDT 2010 data/graphics/playerget.png
11966 Mon Dec 14 00:07:32 PST 2009 data/graphics/playerroll.png
1316 Wed Nov 10 20:12:18 PST 2010 data/graphics/stepswitch.png
2972 Mon Dec 07 23:22:08 PST 2009 data/graphics/sword.png
2972 Mon Dec 07 23:22:08 PST 2009 data/graphics/sword2.png
1450 Sun Nov 07 12:51:22 PST 2010 data/graphics/swordblock.png
10860 Fri Apr 16 07:29:08 PDT 2010 data/graphics/swordget.png
289531 Sun Dec 13 00:28:56 PST 2009 data/graphics/tilemap.png
134821 Tue Dec 15 00:28:38 PST 2009 data/graphics/title.png
6044 Wed Nov 25 17:39:50 PST 2009 data/graphics/win.png
1511 Wed Nov 10 10:38:02 PST 2010 data/graphics/zbutton.png
0 Wed Nov 10 22:32:56 PST 2010 data/level/
2132 Wed Nov 10 20:59:58 PST 2010 data/level/map1.lv
949 Wed Nov 10 22:23:24 PST 2010 data/level/map2.lv
813 Wed Nov 10 21:07:10 PST 2010 data/level/map3.lv
1370 Sun Nov 07 12:37:42 PST 2010 Ammo15.class
1369 Sun Nov 07 12:37:42 PST 2010 BlueGem.class
4789 Sun Nov 07 18:26:34 PST 2010 Boss1.class
825 Mon Nov 08 05:34:02 PST 2010 BossBlock1.class
6159 Sun Nov 07 18:30:54 PST 2010 Bullet.class
3487 Sun Nov 07 12:37:42 PST 2010 ChargeShot.class
8425 Wed Nov 10 22:01:54 PST 2010 Enemy.class
5837 Wed Nov 10 22:01:54 PST 2010 Enemy1.class
6164 Wed Nov 10 22:01:54 PST 2010 Enemy2.class
4902 Sun Nov 07 18:26:34 PST 2010 Enemy3.class
7780 Sun Nov 07 18:26:34 PST 2010 Enemy4.class
30632 Wed Nov 10 22:19:38 PST 2010 Game1.class
656 Tue Nov 09 13:42:32 PST 2010 GrabBlock1.class
1372 Sun Nov 07 12:37:42 PST 2010 GreenGem.class
1074 Tue Nov 09 13:42:34 PST 2010 HexContainer.class
1372 Sun Nov 07 12:37:42 PST 2010 Item.class
811 Wed Nov 10 20:13:46 PST 2010 Key.class
609 Mon Nov 08 20:10:04 PST 2010 Level.class
1368 Sun Nov 07 12:37:42 PST 2010 Life.class
656 Wed Nov 10 16:41:50 PST 2010 LockedBlock.class
2004 Sun Nov 07 18:26:34 PST 2010 Map.class
751 Tue Nov 09 13:42:36 PST 2010 NEBlock.class
659 Tue Nov 09 13:42:36 PST 2010 NormalBlock1.class
18722 Wed Nov 10 22:02:28 PST 2010 Player.class
1726 Mon Nov 08 05:34:06 PST 2010 StepSwitch.class
G:\java>
答案 0 :(得分:5)
一般情况下,因为getClass()的类在com.something.app包中,所以使用相对路径,你的数据必须是com / something / app / data / level。否则使用绝对路径。
要查看jar文件中的内容:
jar tvf myapp.jar
[好吧,经过大量的来回和一些测试后] ......
jcomeau@intrepid:/tmp$ cat test.java; java -cp test.jar test; jar tvf test.jar
import java.io.*;
public class test {
public static void main(String[] args) {
//System.out.println(test.class.getResource("data/level"));
try {
System.out.println(new File(test.class.getResource("data/level").getPath()));
System.out.println(test.class.getResourceAsStream("data/level/test").read());
} catch (Exception whatever) {
System.err.println("error: " + whatever);
}
}
}
file:/tmp/test.jar!/data/level
116
0 Thu Nov 11 00:36:14 PST 2010 META-INF/
40 Thu Nov 11 00:36:14 PST 2010 META-INF/MANIFEST.MF
1362 Thu Nov 11 00:35:46 PST 2010 test.class
0 Wed Nov 10 07:53:18 PST 2010 data/
0 Wed Nov 10 07:53:28 PST 2010 data/level/
5 Wed Nov 10 07:53:28 PST 2010 data/level/test
jcomeau@intrepid:/tmp$
我太累了不能继续,但是main()打印116,这是ASCII值“t”,read()的结果读取第一个字节。这应该足以证明getResourceAsStream()将使您能够访问您的资源。应该有一种方法可以使用File(),但返回的路径很奇怪'!'将路径分隔到 jarfile,路径在 jarfile中。我以前从未处理过这个问题。
答案 1 :(得分:1)
Enumeration<URL> resources = getClass().getClassLoader().getResources("data/level");
System.out.println("exists = " + resources.hasMoreElements());