Eclipse 使用外部库创建 java 可执行文件
我在这个论坛上看到过这个主题,但我需要一个关于如何做到这一点的更基本的解释。
我用 Java 编写了一个程序,其中包含一些外部库(LWJGL 和 Slick)。
这就是我所做的,但我的程序无论如何都不会启动,请告诉我哪里做错了。
我有 Eclipse 3.7.1
- 我的项目在 Eclipse 中打开并在 Eclipse 中运行良好
- 我单击文件 ->导出
- 我选择Java ->可运行的 JAR 文件
- 这里我不知道在启动配置中选择什么,当我单击下拉列表时,我可以选择选择我的主类,所以我这样做了。
- 我选择导出目标
- 我在库处理下选择选项“将所需的库打包到生成的 JAR 中”
- 我不知道 ANT 脚本是什么,所以我不使用它
- 我单击“完成”
- 我将图像文件夹复制到与生成 JAR 文件
- 我尝试启动 JAR 文件,某些内容在后台加载,但什么也没发生,没有窗口显示,什么也没有。
- 我检查 Windows 中的任务管理器,发现 javaw.exe 正在运行
我错过了什么?
我的程序使用图像作为图形,如下所示:
image = new Image("images/filname.png");
我想知道在导出之前是否需要更改路径或加载这些路径的方法?
谢谢!
I've seen this topic in this forum but it I need a more basic explanation on how to do this.
I've done a program in Java with some external libraries (LWJGL and Slick).
So this is what I've done and my program won't start anyway, tell me where I've done wrong.
I have Eclipse 3.7.1
- My project is opened in Eclipse and runs well in Eclipse
- I click File -> Export
- I select Java -> Runnable JAR file
- Here I don't know what to choose in Launch configuration, when I click the dropdown I get the option to choose my main class so I do that.
- I select an export destination
- I select the option "Package required libraries into generated JAR" under Library Handling
- I don't know what ANT script is so I don't use that
- I click Finish
- I copy my images-folder to the same location as the generate JAR-file
- I try to start the JAR-file, something loads in the background but nothing happens, no window shows up, nothing.
- I check the Task manager in windows and sees that a javaw.exe is running
What did I miss?
My program uses images for graphics like this:
image = new Image("images/filname.png");
I wonder if I need to change the paths before exporting or the method to load these?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想从 jar 文件中获取内容,您需要将它们放在类路径中并将它们作为资源访问。
您使用的构造函数指的是无法查看 jar 文件内部的物理文件。
(编辑)另请注意,您无法保证当前工作目录在哪里。因此,如您所见,任何相对引用都可能会中断。
If you want to get things from inside a jar file you need to have them in your classpath and access them as resources.
The constructor you use, refers to a physical file which cannot peek inside a jar-file.
(EDIT) Also note that you have no guarantee where the current working directory is. Hence any relative references may break, as you see.
阅读本文了解如何使用 JarSplice 将 Eclipse 项目导出到可运行的 jar 文件。关于图像,您可以将它们与类文件(在jar中)放在同一目录中,然后编写
to retrieve an InputStream of the image. Then you can load it however you like.
Read this to learn how to use JarSplice to export your Eclipse project to a runnable jar-file. Regarding images, you can put them in the same directory as a class file (in the jar) and then write
to retrieve an InputStream of the image. Then you can load it however you like.
我遇到了同样的问题,并且能够解决它。我所做的只是将数据文件夹(其中包含我的资源)复制到 *.jar 文件中。例如,您可以使用 WinRAR 来执行此操作。
I had the same problem and I was able to fix it. All I did was copy the data folder (which contains my resources) into the *.jar file. You can do this for example with WinRAR.