无法从java swing app

时间:2017-03-13 07:30:05

标签: java eclipse jar executable-jar

我已经创建了一个Java Swing应用程序,现在我想将其导出为可执行jar文件。我在中创建了应用程序,它具有以下结构: enter image description here 文件夹mysqlconnector还包含jar文件。我做了以下几点:   1。首先,我按照this link中的说明,尝试从Runnable jar将其导出为Eclipse。我创建了seo.jar,但是当我尝试通过java -jar seo.jar从终端执行它时出现错误:

  

错误:找不到文件connectionprops.properties

我检查了jar,但我发现它不包含文件connectionprops.properties。即使我手动connectionprops.properties添加到我的jar文件,问题仍然存在。

2. 然后我尝试制作一个名为manifest的{​​{1}}文件,内容为:

manifest.mf

我将Main-Class: bin.main.MainClass //also tried Main-Class: MainClass 放在文件夹manifest.mf中,并在终端中提供了以下命令:

seo

创建jar -cvfm seo.jar manifest.mf * 后,命令:

jar

给我以下输出:

jar -tvf seo.jar

但是当我从终端0 Wed Mar 15 09:13:26 EET 2017 META-INF/ 101 Wed Mar 15 09:13:26 EET 2017 META-INF/MANIFEST.MF 0 Fri Dec 09 14:00:06 EET 2016 arial-unicode-ms/ 23275812 Fri Dec 09 14:00:06 EET 2016 arial-unicode-ms/ARIALUNI.TTF 2426 Fri Dec 09 14:00:06 EET 2016 arial-unicode-ms/arial-unicode-ms.png 98 Fri Dec 09 14:00:06 EET 2016 arial-unicode-ms/myfontsfree.txt 23275812 Fri Dec 09 13:59:58 EET 2016 ARIALUNI.TTF 0 Mon Mar 13 11:35:16 EET 2017 bin/ 0 Mon Mar 13 09:03:32 EET 2017 bin/gui/ 5702 Mon Mar 13 09:03:32 EET 2017 bin/gui/GuiFrame.class 73666 Mon Mar 13 09:03:24 EET 2017 bin/gui/img.png 0 Wed Mar 15 09:10:16 EET 2017 bin/main/ 1961 Mon Mar 13 09:03:32 EET 2017 bin/main/ReadProperties.class 1007 Mon Mar 13 11:30:24 EET 2017 bin/main/MainClass.class 0 Mon Mar 13 11:29:12 EET 2017 bin/main/resources/ 157 Mon Mar 13 11:29:12 EET 2017 bin/main/resources/connectionprops.properties 0 Mon Mar 13 09:03:32 EET 2017 bin/listeners/ 5395 Mon Mar 13 09:03:32 EET 2017 bin/listeners/RegisterIdListener.class 5065 Mon Mar 13 09:03:32 EET 2017 bin/listeners/PayIdListener.class 5081 Mon Mar 13 09:03:32 EET 2017 bin/listeners/StarToBirdListener.class 5095 Mon Mar 13 09:03:32 EET 2017 bin/listeners/DeleteStarListener.class 648 Mon Mar 13 09:03:32 EET 2017 bin/listeners/GoBackListener.class 2707 Mon Mar 13 09:03:32 EET 2017 bin/listeners/DeleteListener.class 73666 Mon Mar 13 09:03:24 EET 2017 bin/listeners/img.png 2612 Mon Mar 13 09:03:32 EET 2017 bin/listeners/NewYearListener.class 6023 Mon Mar 13 09:03:32 EET 2017 bin/listeners/PassListener.class 6710 Mon Mar 13 09:03:32 EET 2017 bin/listeners/AllListener.class 3523 Mon Mar 13 09:03:32 EET 2017 bin/listeners/PdfListener.class 7011 Mon Mar 13 09:03:32 EET 2017 bin/listeners/NewStarListener.class 6620 Mon Mar 13 09:03:32 EET 2017 bin/listeners/PaidIdListener.class 2644 Mon Mar 13 09:03:32 EET 2017 bin/listeners/PayListener.class 6737 Mon Mar 13 09:03:32 EET 2017 bin/listeners/GotIdListener.class 6671 Mon Mar 13 09:03:32 EET 2017 bin/listeners/ToBirdsListener.class 5474 Mon Mar 13 09:03:32 EET 2017 bin/listeners/ReportsListener.class 5385 Mon Mar 13 09:03:32 EET 2017 bin/listeners/NewListener.class 2892 Mon Mar 13 09:03:32 EET 2017 bin/listeners/RegisterListener.class 0 Mon Mar 13 11:35:16 EET 2017 bin/META-INF/ 53 Mon Mar 13 11:35:16 EET 2017 bin/META-INF/MANIFEST.MF 73666 Mon Mar 13 09:03:24 EET 2017 bin/img.png 0 Mon Mar 13 11:35:18 EET 2017 classes/ 0 Mon Mar 13 11:35:18 EET 2017 classes/artifacts/ 0 Mon Mar 13 11:35:18 EET 2017 classes/artifacts/seo_jar/ 3406481 Mon Mar 13 11:35:18 EET 2017 classes/artifacts/seo_jar/seo.jar 157 Sun Jan 22 18:26:06 EET 2017 connectionprops.properties 2148384 Mon Nov 14 17:22:10 EET 2016 itextpdf-5.5.4.jar 31 Mon Mar 13 09:27:02 EET 2017 manifest.mf 0 Thu Dec 08 14:32:48 EET 2016 mysqlconnector/ 960374 Fri Oct 17 08:05:36 EEST 2014 mysqlconnector/mysql-connector-java-5.1.34-bin.jar 1358 Fri Mar 10 09:57:22 EET 2017 seo.iml 0 Wed Mar 15 09:11:32 EET 2017 src/ 0 Wed Feb 01 15:01:04 EET 2017 src/gui/ 73666 Fri Dec 09 13:56:32 EET 2016 src/gui/img.png 5617 Wed Feb 01 12:15:26 EET 2017 src/gui/GuiFrame.java 0 Wed Mar 15 09:08:44 EET 2017 src/main/ 601 Fri Mar 03 19:11:24 EET 2017 src/main/MainClass.java~ 1315 Sun Jan 22 18:30:54 EET 2017 src/main/ReadProperties.java 1060 Mon Mar 13 11:30:24 EET 2017 src/main/MainClass.java 0 Mon Mar 13 11:27:02 EET 2017 src/main/resources/ 157 Sun Jan 22 18:26:06 EET 2017 src/main/resources/connectionprops.properties 48 Wed Mar 15 09:08:32 EET 2017 src/main/MANIFEST.MF 0 Fri Mar 10 10:55:40 EET 2017 src/listeners/ 4761 Wed Feb 01 15:33:12 EET 2017 src/listeners/DeleteStarListener.java 5330 Fri Mar 10 10:55:38 EET 2017 src/listeners/PaidIdListener.java 5268 Fri Mar 10 10:55:40 EET 2017 src/listeners/GotIdListener.java 5233 Fri Mar 10 10:55:40 EET 2017 src/listeners/ToBirdsListener.java 5217 Wed Feb 01 15:33:52 EET 2017 src/listeners/RegisterIdListener.java 405 Wed Feb 01 15:32:56 EET 2017 src/listeners/GoBackListener.java 2361 Wed Feb 01 12:13:34 EET 2017 src/listeners/PdfListener.java 5761 Fri Mar 10 10:55:40 EET 2017 src/listeners/AllListener.java 4700 Wed Feb 01 15:33:30 EET 2017 src/listeners/PayIdListener.java 5165 Fri Mar 10 10:15:20 EET 2017 src/listeners/NewListener.java 4717 Wed Feb 01 15:33:18 EET 2017 src/listeners/StarToBirdListener.java 5222 Fri Mar 10 10:12:06 EET 2017 src/listeners/ReportsListener.java 5922 Fri Mar 10 10:17:32 EET 2017 src/listeners/PassListener.java 73666 Fri Dec 09 13:56:32 EET 2016 src/listeners/img.png 2333 Wed Feb 01 12:13:34 EET 2017 src/listeners/NewYearListener.java 6827 Wed Feb 01 15:33:36 EET 2017 src/listeners/NewStarListener.java 2008 Fri Mar 10 10:14:06 EET 2017 src/listeners/PayListener.java 2162 Fri Mar 10 10:15:20 EET 2017 src/listeners/RegisterListener.java 1982 Fri Mar 10 10:15:20 EET 2017 src/listeners/DeleteListener.java 73666 Fri Dec 09 13:56:32 EET 2016 src/img.png 执行时,我收到错误:

  

错误:无法找到或加载主类src.main.MainClass   我究竟做错了什么?我应该改变项目结构中的某些内容吗?我的项目中是否有其他java -jar seo.jar文件存在问题?如何创建可执行文件jar并成功执行它?

1 个答案:

答案 0 :(得分:0)

您的第一种方法很好,但是当存储在jar文件中时,属性文件无法作为文件系统中的文件进行访问。您需要将其作为类路径上的资源进行访问。

第二种方法表明您错误地构建了jar文件。它是一个zip文件,您应该有一个与您的Main-Class条目完全对应的文件。对于“foo.Bar”,文件“foo / Bar.class”必须存在于jar文件中(并具有main方法)。

(编辑:从“jar tvf”输出中,很明显你错误地构建了文件。你有bin/main/MainClass.class而不是main/MainClass.class)。