如何生成没有main方法的jar文件?

我正在使用Eclipse编写我的程序。 我有9个课程,我正在使用applet,我的代码中没有主要方法。

有人告诉我,我需要提交一个“包含源代码的可运行Jar存档”。 不知何故,当我尝试使用Eclipse导出“Runnable Jar File”导出我的程序时,我找不到程序的启动配置。 它在Eclipse中运行得很好,但我无法导出可运行的Jar文件。

有人能告诉我有什么问题吗?

肯尼

据我所知,可执行JAR需要一个main方法。 在清单Main-Class标记中,只能声明主类所在的类(无方法)。 也许以下代码可能是您的解决方案:

package CaesarCodePackage; public class StartClass { public static void main(String [] args) { // create an object of type appletclass AppletClass theApplet = new AppletClass(); theApplet.init(); // invoke the applet's init() method theApplet.start(); // starts the applet // If the applet views something (this is optional) // Create a window (JFrame) and make applet the content pane. javax.swing.JFrame window = new javax.swing.JFrame("Caesar's Cipher"); window.setContentPane(theApplet); window.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); window.pack(); // Arrange the components. window.setVisible(true); // Make the window visible. } } 

如果您不需要Frame来显示applet,只需启动它。

(感谢Haider M. al-Khateeb的代码)

“启动配置”对于具有主方法的类来说只是一种奇特的语言 – 根据您选择的内容,向导将使用以下属性填充您的可运行jar清单:

 Main-Class: example.MainClass 

当然,如果没有此条目,您实际上无法拥有可执行jar。 所以要回答你的问题,你必须创建一个带有main方法的类。 main方法中的逻辑应该以独立模式启动GUI。 然后再次运行“Export Runnable JAR”向导,并选择您创建的类作为启动配置。

您是否尝试将项目导出为JAR(不可运行的JAR)。

我想ti会让你指定自己的MANIFEST文件。 有了这个,您只需要确保MANIFEST.mf文件包含程序的MAIN-Class。 (你需要一个主类来运行JAR)

http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html

http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

例如

 Main-Class: MyPackage.MyClass 

我猜你遇到了一些启动你的jar子的问题

您可以通过单击“运行配置”来构建自定义启动器,其中将jar放在类路径中

新的慰借