缺少图像/文件的可运行 JAR(资源) [英] Runnable JARs missing Images/Files (Resources)

查看:27
本文介绍了缺少图像/文件的可运行 JAR(资源)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将我的代码作为可运行的 JAR 从 Eclipse 导出时,我设置它要抓取的所有文件(例如按钮图像和其他文件)都丢失了,即使它们实际上在 JAR 中也是如此.我在文件前面添加了 getClass().getResource 但是当我尝试运行 JAR 时甚至没有任何反应,有什么建议吗?

When I export my code as runnable JAR from eclipse all the files that I've set it to grab such as button images and other files are missing even though they are actually in the JAR. I've added getClass().getResource in front of the files but then when I try to run the JAR nothing even happens, any suggestions?

推荐答案

似乎你没有把你的东西放在正确的意义上.为了使其工作,请按照下列步骤操作:

Seems like you not putting your stuff in the right sense. In order to make it work, follow these steps :

  1. Project Explorer Tree 中右键单击您的 Project.
  2. 转到 New ->源文件夹,然后将任何Name 提供给Source Folder.
  3. 现在手动将你的东西添加到这个由你创建的 Source Folder 中,就像如果你想添加图像然后创建一个 New Folder,通过手动访问这个 >源文件夹文件系统.
  4. 将此New Folder 命名为images 并将您的图像复制到此Folder.
  5. 现在返回到您的 Eclipse IDE 并从 Project Explorer 中Refresh您的 Project,通过右键单击您的项目,您现在可以在此处看到添加的内容提神.
  1. Right-Click your Project in Project Explorer Tree.
  2. Go to New -> Source Folder and then provide any Name to the Source Folder.
  3. Now manually add your stuff to this Source Folder so created by you, like if you want to add images then make a New Folder, by manually visiting this Source Folder through File System.
  4. Name this New Folder as images and copy your images to this Folder.
  5. Now go back to your Eclipse IDE and Refresh your Project from the Project Explorer, by Right Clicking your Project, here you be able to see your added content now after refreshing.

现在为了访问,比如说您将使用的任何图像.

Now in order to access, say any image, you will use.

getClass().getResource("/images/yourImageName.extension");

这将返回一个 URL 对象.在这种情况下,请记住 第一个正斜杠,因为在这种情况下,源文件夹中的任何内容都可以在此帮助下访问,更简单地说.现在当你运行你的项目时,这个源文件夹的内容将自动添加到 bin 文件夹中,当你创建一个 Runnable Jar 时,源文件夹中的内容就可以原样访问了.

which will return one URL object. Do remember the first forward slash, in this case, since whatever is inside your Source Folder is accessed with the help of this, in simpler terms. Now when you will Run your project, the content of this Source Folder will be automatically added to the bin folder and when you will create a Runnable Jar, then the stuff inside your Source Folder can be accessed as it is.

这篇关于缺少图像/文件的可运行 JAR(资源)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆