简体   繁体   English

使用 NETBEANS 从 jar 文件中读取资源

[英]Reading resources from jar file with NETBEANS


I am trying to read resources from java class.我正在尝试从 java 类中读取资源。
I have read a lot of information on the net, but not found a good one that solve my problem.我在网上阅读了很多信息,但没有找到解决我问题的好方法。
I wand to set icons dynamically fron png files.我想从 png 文件中动态设置图标。
On netbeans 8.2/Windows 7 the solution used is working在 netbeans 8.2/Windows 7 上使用的解决方案正在运行
But when I create a jar for the program, with the same icon path I got a null value for the url.但是当我为程序创建一个 jar 时,使用相同的图标路径我得到了一个空值的 url。

---------- ----------

      pathToImage = "/images2/letter-E-icon.png";                 // OK in netbeans, KO in jar<br/>
  ImageIcon img;<br/>
  putConsole("pathToImage= "+pathToImage);<br/>

  URL url = this.getClass().getResource(pathToImage);<br/>
  putConsole("url= "+url);<br/>
  img = new ImageIcon(url); <br/>
  JmenuItem_ENC.setIcon(img);<br/>

Below is the source structure and after the jar structure I suspect a problem with netbeans.下面是源结构,在 jar 结构之后,我怀疑 netbeans 有问题。 Can you help me?你能帮助我吗? Thanks谢谢

enter image description here在此处输入图片说明

*** VARIOUS STRUCTURES ****** *** 各种结构 *******
1) Source files 1) 源文件
\\dev\\java\\MyCrypt_RC1 \\dev\\java\\MyCrypt_RC1
build建造
3ÿ883 build.xml 3ÿ883 build.xml
dist
nbproject项目
src源文件
test测试
\\dev\\java\\MyCrypt_RC1\\build \\dev\\java\\MyCrypt_RC1\\build
68 built-jar.properties 68个内置jar.properties
classes班级
depcache缓存
empty空的
generated-sources生成源
\\dev\\java\\MyCrypt_RC1\\build\\classes \\dev\\java\\MyCrypt_RC1\\build\\classes
\\dev\\java\\MyCrypt_RC1\\build\\classes\\enc_dec \\dev\\java\\MyCrypt_RC1\\build\\classes\\enc_dec
7ÿ688 AES.class 7ÿ688 AES.class
3ÿ872 APP_CNST.class 3ÿ872 APP_CNST.class
2ÿ243 CryptAES.class 2ÿ243 CryptAES.class
502 CryptoException.class 502 CryptoException.class
726 DisplayFile$1.class 726 显示文件$1.class
8ÿ001 DisplayFile.class 8ÿ001 DisplayFile.class
\\dev\\java\\MyCrypt_RC1\\build\\classes\\images2 \\dev\\java\\MyCrypt_RC1\\build\\classes\\images2
BUILD all icons .....构建所有图标.....
756 add_entry-icon.png第756话
\\dev\\java\\MyCrypt_RC1\\build\\classes\\resources \\dev\\java\\MyCrypt_RC1\\build\\classes\\resources
323 admin_properties.properties第323话
\\dev\\java\\MyCrypt_RC1\\dist \\dev\\java\\MyCrypt_RC1\\dist
lib
238ÿ859 MyCrypt_RC1.jar 238ÿ859 MyCrypt_RC1.jar
\\dev\\java\\MyCrypt_RC1\\dist\\lib \\dev\\java\\MyCrypt_RC1\\dist\\lib
35ÿ843 jgoodies-common.jar 35ÿ843 jgoodies-common.jar
122ÿ504 jgoodies-forms.jar 122ÿ504 jgoodies-forms.jar
481ÿ534 log4j-1.2.16.jar 481ÿ534 log4j-1.2.16.jar
348ÿ143 MyLib.jar 348ÿ143 MyLib.jar
\\dev\\java\\MyCrypt_RC1\\nbproject \\dev\\java\\MyCrypt_RC1\\nbproject
80ÿ592 build-impl.xml 80ÿ592 build-impl.xml
475 genfiles.properties第475话
private私人的
3ÿ210 project.properties 3ÿ210 project.properties
565 project.xml第565话
\\dev\\java\\MyCrypt_RC1\\src \\dev\\java\\MyCrypt_RC1\\src
enc_dec enc_dec
images2图片2
resources资源
\\dev\\java\\MyCrypt_RC1\\src\\enc_dec \\dev\\java\\MyCrypt_RC1\\src\\enc_dec
...... all java code .......... ......所有java代码......
\\dev\\java\\MyCrypt_RC1\\src\\images2 \\dev\\java\\MyCrypt_RC1\\src\\images2
SRC all icons SRC 所有图标
756 add_entry-icon.png第756话
1ÿ211 xopen_all.png 1ÿ211 xopen_all.png
\\dev\\java\\MyCrypt_RC1\\src\\resources \\dev\\java\\MyCrypt_RC1\\src\\resources
other resources from properties来自物业的其他资源
323 admin_properties.properties第323话
======================================================================= ================================================== ======================
2) Jar file hierarchy 2) Jar 文件层次结构
\\dev\\java\\MyCrypt_RC1\\dist\\zip \\dev\\java\\MyCrypt_RC1\\dist\\zip
enc_dec enc_dec
images2图片2
META-INF元信息
resources资源
\\dev\\java\\MyCrypt_RC1\\dist\\jar\\enc_dec \\dev\\java\\MyCrypt_RC1\\dist\\jar\\enc_dec
.... All classes ..... ....所有类.....
\\dev\\java\\MyCrypt_RC1\\dist\\jar\\images2 \\dev\\java\\MyCrypt_RC1\\dist\\jar\\images2
.... All icons ..... .... 所有图标 .....

756 add_entry-icon.png第756话
\\dev\\java\\MyCrypt_RC1\\dist\\jar\\META-INF \\dev\\java\\MyCrypt_RC1\\dist\\jar\\META-INF
243 MANIFEST.MF第243话
\\dev\\java\\MyCrypt_RC1\\dist\\jar\\resources \\dev\\java\\MyCrypt_RC1\\dist\\jar\\resources
323 admin_properties.properties第323话

I have been busy to solve this problem.我一直在忙着解决这个问题。 I restarted all freshly with a new project and I manage to find the correct solution.我重新启动了一个新项目,并设法找到了正确的解决方案。


In the following figure, I indicate what is working for the directory structure:在下图中,我指出了目录结构的作用:

Thanks for the help This is the directory structure (clik)感谢您的帮助这是目录结构(点击)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM