gpt4 book ai didi

java - 创建包含外部文本和 ser 文件的可运行 Jar

转载 作者:太空宇宙 更新时间:2023-11-04 06:34:09 24 4
gpt4 key购买 nike

好吧,我尝试了很多方法来创建可执行 jar,但 4 小时后我放弃了。我正在尝试使用我的项目制作可执行的 jar 文件,其中主类位于 Controller 中。基本上我尝试使用导出/可运行的 JAR 文件和各种选项,包括生成 ant 脚本。我在终端中总是遇到类似的错误

当我使用终端运行该命令时

java -jar fruitStore.jar

它说

    bmj@OZ:~/Documents$ java -jar fruitStoreApp.jar 
java.io.FileNotFoundException: customers.ser (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at model.Shop.loadCustomers(Shop.java:250)
at model.Shop.<init>(Shop.java:121)
at model.Shop.getInstance(Shop.java:140)
at model.Model.<init>(Model.java:12)
at GUI.CustomerMainPanel.<init>(CustomerMainPanel.java:24)
at GUI.MainMenuGUI.<init>(MainMenuGUI.java:50)
at GUI.MainMenuGUI.getInstance(MainMenuGUI.java:35)
at model.View.<init>(View.java:49)
at model.Controller.<init>(Controller.java:42)
at model.Controller.main(Controller.java:805)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
java.io.FileNotFoundException: users.ser (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at model.Shop.loadUsers(Shop.java:210)
at model.Shop.<init>(Shop.java:122)
at model.Shop.getInstance(Shop.java:140)
at model.Model.<init>(Model.java:12)
at GUI.CustomerMainPanel.<init>(CustomerMainPanel.java:24)
at GUI.MainMenuGUI.<init>(MainMenuGUI.java:50)
at GUI.MainMenuGUI.getInstance(MainMenuGUI.java:35)
at model.View.<init>(View.java:49)
at model.Controller.<init>(Controller.java:42)
at model.Controller.main(Controller.java:805)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
java.io.FileNotFoundException: suppliers.ser (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at model.Shop.loadSuppliers(Shop.java:190)
at model.Shop.<init>(Shop.java:123)
at model.Shop.getInstance(Shop.java:140)
at model.Model.<init>(Model.java:12)
at GUI.CustomerMainPanel.<init>(CustomerMainPanel.java:24)
at GUI.MainMenuGUI.<init>(MainMenuGUI.java:50)
at GUI.MainMenuGUI.getInstance(MainMenuGUI.java:35)
at model.View.<init>(View.java:49)
at model.Controller.<init>(Controller.java:42)
at model.Controller.main(Controller.java:805)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
java.io.FileNotFoundException: stocks.ser (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at model.Shop.loadStock(Shop.java:230)
at model.Shop.<init>(Shop.java:124)
at model.Shop.getInstance(Shop.java:140)
at model.Model.<init>(Model.java:12)
at GUI.CustomerMainPanel.<init>(CustomerMainPanel.java:24)
at GUI.MainMenuGUI.<init>(MainMenuGUI.java:50)
at GUI.MainMenuGUI.getInstance(MainMenuGUI.java:35)
at model.View.<init>(View.java:49)
at model.Controller.<init>(Controller.java:42)
at model.Controller.main(Controller.java:805)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
java.io.FileNotFoundException: sales.ser (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at model.Shop.loadSales(Shop.java:150)
at model.Shop.<init>(Shop.java:126)
at model.Shop.getInstance(Shop.java:140)
at model.Model.<init>(Model.java:12)
at GUI.CustomerMainPanel.<init>(CustomerMainPanel.java:24)
at GUI.MainMenuGUI.<init>(MainMenuGUI.java:50)
at GUI.MainMenuGUI.getInstance(MainMenuGUI.java:35)
at model.View.<init>(View.java:49)
at model.Controller.<init>(Controller.java:42)
at model.Controller.main(Controller.java:805)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
java.io.FileNotFoundException: orders.ser (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at model.Shop.loadOrders(Shop.java:170)
at model.Shop.<init>(Shop.java:127)
at model.Shop.getInstance(Shop.java:140)
at model.Model.<init>(Model.java:12)
at GUI.CustomerMainPanel.<init>(CustomerMainPanel.java:24)
at GUI.MainMenuGUI.<init>(MainMenuGUI.java:50)
at GUI.MainMenuGUI.getInstance(MainMenuGUI.java:35)
at model.View.<init>(View.java:49)
at model.Controller.<init>(Controller.java:42)
at model.Controller.main(Controller.java:805)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1301)
at GUI.WelcomeScreenGUI.<init>(WelcomeScreenGUI.java:33)
at model.View.<init>(View.java:51)
at model.Controller.<init>(Controller.java:42)
at model.Controller.main(Controller.java:805)
... 5 more

我知道问题出在哪里了。它不会将图片上标记的文件附加到 jar

picture with needed files

我的问题是:如何添加该文件?您能给我详细的答案吗?一个条件。我无法更改该文件的位置。

我读到了一些 Maven 解决方案,由于缺乏 Maven 知识,我想避免使用这些解决方案。

提前感谢您的快速回复

最佳答案

查看您的代码后,我发现您已设置文件名,并且正在使用从文件系统读取的 FileInputStream

private String saleFile = "sales.ser";
...
public void loadSales(String inPutFile) {
try {
FileInputStream fileIn = new FileInputStream(inPutFile);

因此,程序将在当前工作目录中查找 sale.ser。在您的 IDE 中,工作目录通常是项目根目录,这是您的文件所在的位置,这就是它工作的原因。

既然您无法更改代码,这里有一些建议。

运行 jar 时,需要从当前工作目录运行 jar。导出 jar 时,将其导出到项目根目录。当您运行时,请确保您位于项目根目录中

可能更好的方法是编写脚本来启动程序(这很常见)。这将为您启动应用程序的方式/地点提供更大的灵 active 。我不是最擅长编写脚本,所以我只会写最基本的脚本,但您可能希望根据您想要启动应用程序和查找资源的方式将其变得更复杂。对于下面的 .bat 文件示例,.bat 文件也应该位于项目根目录中

@echo off
setlocal

if "%JAVACMD%" == "" set JAVACMD=%JAVA_HOME%\bin\java.exe
if "%FRUITSTAND_HOME%" == "" set FRUITSTAND_HOME=%~dp0

cd "%FRUITSTAND_HOME%"
"%JAVACMD%" -cp "%FRUITSTAND_HOME%\lib\*" -jar "%FRUITSTAND_HOME%\fruitStoreApp.jar"
endlocal

然后(从项目根目录)只需运行 fruitStoreApp.bat

我测试过,它有效。我刚刚收到 stocks.ser sales.serorder.serFileNotFoundException,因为……它们不存在(在你的项目中 - 至少在你的 github 中不存在)。

看起来您正在使用 bash,因此您将编写一个简单的 bash 脚本。我将创建 .bat.sh 文件(这很常见),以允许 Windows 和 Unix 用户访问

关于java - 创建包含外部文本和 ser 文件的可运行 Jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25647845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com