简体   繁体   English

可运行的JAR文件不会

[英]Runnable JAR file won't

I created this card game in Java. 我用Java创建了这个纸牌游戏。 What it does it it presents one card face up and 4 more cards face down. 它的作用是使一张卡面朝上,另外四张面朝下。 You wager from 1 to 100 coins and try to pick a higher card from the face down cards. 您下注1到100个硬币,并尝试从正面朝下的纸牌中选择更高的纸牌。 If you pick a higher card, your wager is doubled and you can choose to go double or nothing on another round. 如果您选择更高的卡,您的赌注将加倍,并且您可以选择在下一轮中加倍或全无。

The program uses 3 .java files in one package: 该程序在一个包中使用3个.java文件:

  • HigherNumber: Main class, contains the bulk of the code. HigherNumber:主类,包含大部分代码。
  • Deck: Contains definition for a class representing a deck of cards. Deck:包含代表一副纸牌的类的定义。
  • Card: Contains definition for a class representing an individual card. 卡:包含代表单个卡的类的定义。

So naturally, this program uses a lot of pictures, to represent the cards. 因此,该程序自然使用大量图片来表示卡片。 In my original implementation, I just passed ImageIcon a string to represent the location of the cards. 在我最初的实现中,我只是向ImageIcon传递了一个字符串,以表示卡的位置。 So like, for the icon for a face down card, 就像,对于一张面朝下的卡片的图标,

faceDown = new ImageIcon("multimedia/redBack.gif");

When I did this, the program ran perfectly when run through Eclipse. 当我这样做时,该程序在Eclipse中运行时运行完美。 So I used Eclipse to Export to a runnable JAR file. 因此,我使用Eclipse导出到可运行的JAR文件。 This JAR file then ran without a problem, except if I moved the JAR file anywhere else, none of the images showed up. 然后,该JAR文件运行没有问题,除非我将JAR文件移动到其他地方,所有图像均未显示。

So I researched and found out about using URLs to combat this. 因此,我研究并找到了使用URL来解决此问题的方法。 I reworked the program to use URLs, so now I have stuff like this: 我对该程序进行了重新设计以使用URL,所以现在有了这样的内容:

//Set URL for default faceDown icon.
faceDownURL = this.getClass().getResource(pictureRoot +"redBack.gif");
//Set location for default back face of cards.
faceDown = new ImageIcon(faceDownURL);

Now it runs fine in Eclipse, but I cannot get the exported runnable JAR to work. 现在它可以在Eclipse中正常运行,但是我无法使导出的可运行JAR正常工作。 When run from Windows, it just kinda blinks and does nothing. 从Windows运行时,它只是闪烁而没有执行任何操作。 When I run through the command line, I get this: 当我通过命令行运行时,得到以下信息:

C:\Documents and Settings\mstabosz>java -jar C:\Temp\HigherNumber.jar
Exception in thread "main" java.lang.NullPointerException
        at javax.swing.ImageIcon.<init>(Unknown Source)
        at higherNumber.Card.setImage(Card.java:150)
        at higherNumber.Card.<init>(Card.java:36)
        at higherNumber.Deck.<init>(Deck.java:22)
        at higherNumber.HigherNumber.<init>(HigherNumber.java:16)
        at higherNumber.HigherNumber.main(HigherNumber.java:857)

Trying to follow this code, it looks like the source of the problem is in the Card class at line 150. At line 150, the class is in the setImage() function, which is building a string called iconName to be used to set the image for each card as it is created. 尝试遵循此代码,问题的根源似乎在第150行的Card类中。在第150行,该类在setImage()函数中,该函数正在构建名为iconName的字符串以用于设置每张卡创建时的图像。 It then returns an ImageIcon to the Card class's constructor. 然后,它将ImageIcon返回给Card类的构造函数。

//Set up the icon for the card.
this.cardIcon = setImage();

Line 150 is the return statement. 第150行是return语句。 Here are the statements that create the URL cardIconURL which is used in the ImageIcon. 以下是创建ImageIcon中使用的URL cardIconURL的语句。

//Create a URL based on the constructed string.
URL cardIconURL = this.getClass().getResource(iconName);

return new ImageIcon(cardIconURL);

I just don't get what's going wrong here. 我只是不明白这里出了什么问题。 The program worked fine as a runnable JAR when I was using Strings instead of URLs. 当我使用字符串而不是URL时,该程序作为可运行的JAR正常运行。 It works fine when run through Eclipse. 在Eclipse中运行时,效果很好。 It doesn't work as a runnable JAR now. 它现在不能用作可运行的JAR。

I did read up on something called manifests, which I had trouble understanding. 我确实阅读了一些清单,但我很难理解。 I did have Eclipse generate a manifest for this program: 我确实让Eclipse为该程序生成了清单:

Manifest-Version: 1.0
Main-Class: higherNumber.HigherNumber

What am I missing? 我想念什么?

i use something like: 我用类似的东西:

URL myurl = this.getClass().getResource("file.png");
myIconImage = Toolkit.getDefaultToolkit().getImage(myurl);

you're doing: 你在做:

return new ImageIcon(cardIconURL);

Maybe try my second line. 也许尝试我的第二行。 Also i store the images in the jar. 我也将图像存储在罐子中。

Okay it looks like I got the image files in the runnable JAR by dragging and dropping the "multimedia" folder which contains the pictures into the "highernumber" package in Eclipse. 好的,看起来好像是通过将包含图片的“多媒体”文件夹拖放到Eclipse的“更高编号”包中,将图像文件保存在可运行的JAR中。 I'm still getting the NullPointerException though. 我仍然得到NullPointerException。

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

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