简体   繁体   English

Java Swing:包含图像时,可运行的JAR文件不起作用

[英]Java Swing: Runnable JAR file does not work when including images

The code below works fine in Eclipse (both image-handling possibilities). 下面的代码在Eclipse中可以正常工作(两种图像处理方式都可以)。 But when exporting as a Runnable JAR File, and double-clicking the .JAR, nothing happens. 但是,当导出为可运行的JAR文件并双击.JAR时,没有任何反应。 If I comment out the image parts of the code, the .JAR runs fine as an export and the frame builds. 如果我注释掉代码的图像部分,则.JAR在导出时运行良好,并且会构建框架。 So it seems the getting of the image is causing the .JAR to fail. 因此,图像的获取似乎导致.JAR失败。

I've got the strawberry.jpg file sitting in 'C:\\Users\\sean\\workspace\\myApps\\bin\\testing' Could you advise if the issue is with my code? 我已经将Strawberry.jpg文件放在“ C:\\ Users \\ sean \\ workspace \\ myApps \\ bin \\ testing”中,请问问题出在我的代码上吗?

(Code first modified here: Java Swing: unable to load image using getResource ) (代码首先在这里修改: Java Swing:无法使用getResource加载图像

package testing;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;



public class IconTest {


public static void main(String[] arguments) throws IOException {

    JFrame frame1 = new JFrame();
    frame1.setTitle("Frame1");
    frame1.setSize(500, 500);
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    FlowLayout flo = new FlowLayout();
    frame1.setLayout(flo);

    //POSSIBILITY 1 TO HANDLE IMAGE
      InputStream resourceAsStream = IconTest.class.getResourceAsStream("strawberry.jpg");
    Image image = ImageIO.read(resourceAsStream);
    JLabel label1 = new JLabel(new ImageIcon(image));

    //POSSIBILITY 2 TO HANDLE IMAGE
/*    java.net.URL url= IconTest.class.getResource("strawberry.jpg");
    BufferedImage watermarkImage = ImageIO.read(url);
    JLabel label1 = new JLabel(new ImageIcon(watermarkImage));*/


    frame1.add(label1);
    frame1.setVisible(true);
}

} }

POSSIBILITY 3 TO HANDLE IMAGE : 处理图像的可能性3:

You can check that resource that you are fetching is exist or not. 您可以检查正在获取的资源是否存在。 It resource not found then you can create JLabel with alternative text also... 找不到资源,然后您还可以使用替代文本创建JLabel ...

JLabel label1 ;
URL imageUrl = IconTest.class.getClassLoader().getResource("strawberry.jpg");
if ( imageUrl != null ) {
    label1 = new JLabel(new ImageIcon(imageUrl));
} else {
    label1 = new JLabel("Alternative text");
}

Put the image where you have the source file because it points to the source directory 将图像放在您具有源文件的位置,因为它指向源目录

InputStream resourceAsStream = IconTest.class.getResourceAsStream("strawberry.jpg");

and then generate runnable jar file and then right click the jar and give run as java platform binary 然后生成可运行的jar文件,然后右键单击该jar并给出以Java平台二进制文件的身份运行

Following Maven standard directory layout might be helpful: 遵循Maven标准目录布局可能会有所帮助:

布局

InputStream resourceAsStream = IconTest.class.getResourceAsStream("/strawberry.jpg")

It should start from "/". 它应从“ /”开始。 Also check whether the jar actually have the image. 还要检查罐子上是否确实有图像。 Just unpack it and check whether the image is actually added. 只需打开包装并检查是否已实际添加图像。

The guidance offered here in troubleshooting the problem helped greatly - thank you to everyone. 此处提供的有关解决问题的指南很有帮助-谢谢大家。 I tested contents of an exported JAR; 我测试了导出的JAR的内容; I used diagnostic code suggested by Kishan to determine if my code could "see" the image. 我使用Kishan建议的诊断代码来确定我的代码是否可以“看到”图像。 I believe it might have something to do with the way Eclipse works/refreshes if I move images around in the file system instead of the Eclipse import function. 我相信,如果我在文件系统中而不是Eclipse导入功能中移动图像,这可能与Eclipse工作/刷新的方式有关。 Finally, in order to get it to work, I made the following changes: I created a new project and two sub-packages - one for 'resources' and one for my code class. 最后,为了使其正常工作,我进行了以下更改:我创建了一个新项目和两个子包-一个用于“资源”,另一个用于我的代码类。 Then I right-clicked > import on the resources package in Eclipse to get the image. 然后,我右键单击>导入Eclipse中的资源包以获取图像。

The only thing to change in my code is ....getResourceAsStream("/resources/strawberry.jpg"); 我的代码中唯一需要更改的是.... getResourceAsStream(“ / resources / strawberry.jpg”);

暂无
暂无

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

相关问题 当我将Java导出为可运行的jar文件时,不起作用 - When i export my java as runnable jar file, do not work Java Swing:从jar文件运行时图像不显示 - Java Swing: Images not Displaying when run from jar file Java,创建仅包含依赖项的可运行jar文件。 - Java, create runnable jar file including ONLY used dependencies. 在Java中,为什么class.getResource(“ / path / to / res”); 将文件复制到系统时,无法在可运行的jar中工作? - In Java, why does class.getResource(“/path/to/res”); not work in the runnable jar when copying files to the system? 在Eclipse中播放视频效果很好,但是当将Java项目导出为可运行的JAR文件时,它不再起作用 - Playing videos works fine in Eclipse but when exporting the java project as an runnable JAR file it doesnt work anymore 包括runnable jar文件中的源文件 - including source files in runnable jar file 从.jar文件运行时无法在Java Swing中加载图像 - Can not load the images in Java Swing when running from a .jar-file Java Swing Gui 无法从可运行的 jar 文件中读取数据 - Java Swing Gui is not able to read data from the file inside the runnable jar 使用 Java Swing 和集成的 JavaFX 通过 Maven 创建可运行的 .jar - 实例化 JFXPanel 时应用程序崩溃 - Creating a runnable .jar with Maven using Java Swing and integrated JavaFX - App crashes when JFXPanel is being instantiated Java项目在Eclipse上工作但在导出到runnable jar文件时不起作用 - Java project working on Eclipse but not working when exported to runnable jar file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM