简体   繁体   English

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

[英]How to generate jar file with no main method?

I am using Eclipse to write my program. 我正在使用Eclipse编写程序。 I have 9 classes and I'm using applet and there is no main method within my codes. 我有9个课程,并且我使用的是applet,我的代码中没有main方法。

I have been told that I need to submit a "runnable Jar Archive including source code". 有人告诉我,我需要提交“可运行的Jar存档,包括源代码”。 Somehow when I try to export my program using the Eclipse export "Runnable Jar File", I cannot find the launch configuration of my program. 以某种方式,当我尝试使用Eclipse导出“ Runnable Jar File”导出程序时,找不到程序的启动配置。 It works perfectly fine within Eclipse but I just can't export a runnable Jar File. 它在Eclipse中工作得很好,但是我无法导出可运行的Jar文件。

Can someone tell me what is the problem please? 有人可以告诉我是什么问题吗?

Kenny 肯尼

The 'Launch Configuration' is just fancy language for a class with a main method - based on what you select the wizard will populate your runnable jar manifest with the following attribute: “启动配置”只是具有主要方法的类的特殊语言-基于您选择的向导,向导将使用以下属性填充可运行的jar清单:

Main-Class: example.MainClass

Of course, you can't actually have an executable jar without this entry. 当然,没有此条目,您实际上就无法拥有可执行jar。 So to answer your question, you have to create a class with a main method in it. 因此,要回答您的问题,您必须创建一个带有main方法的类。 The logic in the main method should launch your GUI in standalone mode. main方法中的逻辑应以独立模式启动GUI。 Then run your 'Export Runnable JAR` wizard again and choose the class you created as the Launch Configuration. 然后再次运行“导出可运行JAR”向导,然后选择您创建的类作为“启动配置”。

i guess you meet some problem launching your jar 我猜你在启动罐子时遇到了问题

you can build your custom launcher by click 'Run configurations' , where put your jar in classpath 您可以通过单击“运行配置”来构建自定义启动器,将jar放在类路径中

新配置

As far as I know an executable JAR needs a main method. 据我所知,可执行JAR需要一种主要方法。 In the manifest Main-Class tag only a class can be declared (no method) where the the main-class is located. 在清单Main-Class标记中,只能声明主类所在的类(无方法)。 Maybe the following code is a possible solution for you: 也许以下代码为您提供了可能的解决方案:

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.
    }
}

If you do not need a Frame for showing the applet only start it. 如果不需要用于显示小程序的框架,则仅启动它。

(Thanks to Haider M. al-Khateeb for the code) (感谢Haider M. al-Khateeb提供的代码)

Have you tried exporting the project as a JAR (Not runnable JAR). 您是否尝试过将项目导出为JAR(不可运行的JAR)。

I think ti will then let you specify your own MANIFEST file. 我认为ti然后可以让您指定自己的清单文件。 With this you simply need to make sure the MANIFEST.mf file contains a MAIN-Class for the program. 这样,您只需要确保MANIFEST.mf文件包含该程序的MAIN-Class。 (You will need a main class for the JAR to run) (您需要一个主类来运行JAR)

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

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

eg 例如

Main-Class: MyPackage.MyClass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM