简体   繁体   English

在netbeans中创建具有多个主类的jar

[英]Creating jars with multiple main classes in netbeans

I'm am creating a VoIP client server pair in java using the netbeans IDE. 我正在使用netbeans IDE在java中创建VoIP客户端服务器对。 I would like to use only 1 project but I am unfamiliar with with how netbeans would create the Jar file. 我想只使用一个项目,但我不熟悉netbeans如何创建Jar文件。 I need a to if this is possible and if so, how to set it up. 我需要一个如果这是可能的,如果是的话,如何设置它。

The JAR File Specification allows only one Main-Class attribute per JAR, but the JAR may have an arbitrary number of classes that declare a main() method. JAR文件规范每个JAR只允许一个Main-Class属性,但JAR可以有任意数量的类来声明main()方法。 Any such class will be included in the project's Build Packaging property, unless specifically excluded. 除非特别排除,否则任何此类都将包含在项目的Build Packaging属性中。

As a concrete example, H2 Database includes these classes with main() : 作为具体示例, H2数据库包含main()这些类:

org.h2.jdbcx.JdbcConnectionPool
org.h2.tools.Backup
org.h2.tools.ChangeFileEncryption
org.h2.tools.Console
org.h2.tools.ConvertTraceFile
org.h2.tools.ConvertTraceFile
org.h2.tools.CreateCluster
org.h2.tools.DeleteDbFiles
org.h2.tools.Recover
org.h2.tools.Restore
org.h2.tools.RunScript
org.h2.tools.Script
org.h2.tools.Server
org.h2.tools.Shell

Addendum: Apparently, my junk-drawer project needs maintenance. 附录:显然,我的垃圾抽屉项目需要维护。

$ find scratch/src -name \*java | xargs -J % egrep 'main[ \t]*\(Str' % | wc -l
     109

I don't know how Netbeans work, but it should be no problem putting more than one main class in a JAR. 我不知道Netbeans是如何工作的,但是在JAR中放置多个主类应该没问题。 Actually a main class is just a class having a main method and a JAR is a collection of class files. 实际上,主类只是一个具有main方法的类,而JAR是一个类文件的集合。
The only restriction is that there can only be one class that will be started with double-clicking the JAR. 唯一的限制是只有一个类可以通过双击JAR来启动。

To start the each class you must not use the -jar option, but provide the full class name. 要启动每个类,您不能使用-jar选项,而是提供完整的类名。
For example, if you have a Client and a Server class in your JAR, the Client is started by 例如,如果JAR中有Client和Server类,则客户端将启动
java -cp file.jar Client
and the Server by 和服务器
java -cp file.jar Server . java -cp file.jar Server

An option is to create a third starter class used to start either the server or the client based on a command line argument (or a GUI window). 一个选项是创建第三个启动类,用于根据命令行参数(或GUI窗口)启动服务器或客户机。

I believe the purpose of a 'main class' means that the eventual JAR' manifest file, will label one class as being the 'class to run': so that the end-user can just double-click it* or run a simplified commandline like: 我相信'主类'的目的意味着最终的JAR'清单文件,将一个类标记为'要运行的类':这样最终用户可以只需双击它*或运行简化的命令行喜欢:

java -jar <jar>

rather than having to specify the whole package/class name like: 而不是必须指定整个包/类名称,如:

java -cp <jar> com.yourcom.package.classname

If I'm right, then I don't see how it would make sense to have more than one main class ? 如果我是对的,那么我不知道拥有多个主要课程会有多大意义? Maybe I misunderstod your question - or there is another purpose to the 'main' class? 也许我误解了你的问题 - 或者'主'课还有另一个目的吗?

If you mean having two classes which have a 'main' method - then this is fine - the end user can launch any of the classes by name - and so long as they have the standard main method sig, for instance: 如果你的意思是有两个具有'main'方法的类 - 那么这很好 - 最终用户可以按名称启动任何类 - 只要他们有标准的main方法sig,例如:

public static void main(String[] args)

it should just work. 它应该工作。

*(on Windows at least, and whether that works also depends on which JRE they have and probably other things) *(至少在Windows上,这是否有效还取决于他们拥有的JRE以及其他可能的东西)

In netbeans, if you want to create jar file of your project with your selected main class then , 1. right click on your project (If you couldn't find your project then go to window -> Project from netbeans menubar ) 2. Go to set configuration -> Customize. 在netbeans中,如果你想用你选择的主类创建你的项目的jar文件,那么,1。 右键单击你的项目 (如果你找不到你的项目,那么转到窗口 - >来自netbeans菜单栏的项目)2。 去设置配置 - >自定义。 3. Select run from categories, In main class write your packagename.classname (ie you have created file xyz in mypackage, then write mypackage.xyz) 4. click ok. 3. 选择从类别运行,在主类中编写packagename.classname (即你在mypackage中创建了文件xyz,然后编写mypackage.xyz)4。 单击确定。
5. Again right click on projectname, select clean and build. 5. 再次右键单击projectname,选择clean并build。 6. Your jar file is created at location of your projectname -> dist -> projectname.jar 6.您的jar文件是在projectname - > dist - > projectname.jar的位置创建的

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

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