[英]Running a Jar file alone
Hai我有一个在netbeans6.0.1中设计的应用程序,需要在其中显示图像作为徽标。我的编码如下
mLogo = new JLabel();
URL lUrlLogo = getclass().getResource("/com/images/image.gif");
Image lLogoimage = Toolkit.getDefaultToolkit().getImage(lUrlLogo);
ImageIcon licon = new ImageIcon(lLogoimage);
mLogo.setIcon(licon);
当我在netbeans中执行它时,我的代码工作正常,但是当我尝试运行由netbeans在dist文件夹中创建的jar文件时,它给了我一个例外
Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(Unknown Source)
at sun.awt.image.URLImageSource.getDecoder(Unknown Source)
at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
at sun.awt.image.ImageFetcher.run(Unknown Source)
我的罐子有什么问题可以帮助我运行我的罐子吗?
并且您已检查JAR文件是否包含路径"/com/images/image.gif"
?
另外,我不确定前导斜线,我总是使用相对路径来访问资源(但是我想无论如何它都应该起作用)。
作为类it.lapo.example.Main
一个示例,我使用getClass().getResource("resources/logo.png")
并且在JAR文件中是这样的:
it/lapo/example/Main.class
it/lapo/example/resources/logo.png
我知道...与您自己的示例相差无几,但确实有效...所以我想您的示例也很接近正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.