如何在java中设置默认主类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1635136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to set default main class in java?
提问by user189352
I have 2 classes within same package. Both classes have main method in them. Now I want to build a jar file. I want to build 2 jar files which use different main functions as default main.
我在同一个包中有 2 个类。这两个类中都有 main 方法。现在我想构建一个 jar 文件。我想构建 2 个 jar 文件,它们使用不同的主要功能作为默认主要功能。
eg
例如
class A
{
public static void main(String args[])
{
//do something
}
}
class B
{
public static void main(String args[])
{
//do something
}
}
How do I do it in NetBeans IDE?
如何在 NetBeans IDE 中执行此操作?
I found the answer. U can do it easily in netbeans: 1)right click on project >properties > run > select the class frm and drop down list. So simple in netbeans. Netbeans rocks!
我找到了答案。您可以在 netbeans 中轻松完成:1) 右键单击项目 > 属性 > 运行 > 选择类 frm 并下拉列表。在netbeans中如此简单。网豆摇滚!
采纳答案by James Black
In the jar file you could just add this to your manifest.mft
在 jar 文件中,您可以将其添加到 manifest.mft
Main-Class : A
The jar file would then be executable and would call the correct main.
然后 jar 文件将是可执行的,并会调用正确的 main.jar 文件。
On how to do this in Netbeans you can look at this: Producing executable jar in NetBeans
关于如何在 Netbeans 中执行此操作,您可以查看: 在 NetBeans 中生成可执行 jar
回答by Spike Williams
You can set the Main-Class attribute in the jar file's manifest to point to which file you want to run automatically.
您可以在 jar 文件的清单中设置 Main-Class 属性以指向要自动运行的文件。
回答by Rahul
Best way is to handle this in an Ant script. You can create 2 different tasks for the 2 jar files. Specify class A as the main class in the manifst file for the first jar. similarly specify class B as the main class in the manifest file for the second jar.
最好的方法是在 Ant 脚本中处理这个问题。您可以为 2 个 jar 文件创建 2 个不同的任务。将类 A 指定为第一个 jar 的清单文件中的主类。类似地,将 B 类指定为第二个 jar 的清单文件中的主类。
you can easily run the Ant tasks from Netbeans.
您可以轻松地从 Netbeans 运行 Ant 任务。
回答by Nate
If you're creating 2 executable JAR files, each will have it's own manifest file, and each manifest file will specify the class that contains the main()
method you want to use to start execution.
如果您要创建 2 个可执行 JAR 文件,每个文件都有自己的清单文件,每个清单文件将指定包含main()
要用于开始执行的方法的类。
In each JAR file, the manifest will be a file with the following path / name inside the JAR - META-INF/MANIFEST.MF
在每个 JAR 文件中,清单将是一个在 JAR 中具有以下路径/名称的文件 - META-INF/MANIFEST.MF
There are ways to specify alternatively named files as a JAR's manifest using the JAR command-line parameters.
有多种方法可以使用JAR 命令行参数将替代命名的文件指定为 JAR 的清单。
The specific class you want to use is specified using Main-Class: package.classnameinside the META-INF/MANIFEST.MF file.
您要使用的特定类是使用META-INF/MANIFEST.MF 文件中的Main-Class: package.classname指定的。
As for how to do this in Netbeans - not sure off the top of my head - I usually use IntelliJ and / or Eclipse and usually build the JAR through ANT or Maven anyway.
至于如何在 Netbeans 中执行此操作 - 不确定我的头脑 - 我通常使用 IntelliJ 和/或 Eclipse,并且通常通常通过 ANT 或 Maven 构建 JAR。
回答by erickson
If the two jars that you want to create are the mostly the same, and the only difference is the main class that should be started from each, you can put all of the classes in a third jar. Then create two jars with just a manifest in each. In the MANIFEST.MF file, name the entry class using the Main-Class
attribute.
如果您要创建的两个 jar 大部分相同,唯一的区别是应该从每个 jar 启动的主类,您可以将所有类放在第三个 jar 中。然后创建两个 jar,每个 jar 中只有一个清单。在 MANIFEST.MF 文件中,使用Main-Class
属性命名条目类。
Additionally, specify the Class-Path
attribute. The value of this should be the name of the jar file that contains all of the shared code. Then deploy all three jar files in the same directory. Of course, if you have third-party libraries, those can be listed in the Class-Path attribute too.
此外,指定Class-Path
属性。这个值应该是包含所有共享代码的 jar 文件的名称。然后将所有三个 jar 文件部署在同一目录中。当然,如果您有第三方库,它们也可以列在 Class-Path 属性中。
回答by Anthony
Assuming your my.jar has a class1 and class2 with a main defined in each, you can just call java like this:
假设你的 my.jar 有一个 class1 和 class2,每个都定义了一个 main,你可以像这样调用 java:
java my.jar class1
java my.jar class2
If you need to specify other options to java just make sure they are before the my.jar
如果您需要为 java 指定其他选项,请确保它们在 my.jar 之前
java -classpath my.jar class1
回答by bos_ahonk
Right-click the project node in the Projects window and choose Project Properties.then find run, there you can setup your main class,, **actually got it from netbeans default help
右键单击“项目”窗口中的项目节点,然后选择“项目属性”。然后找到运行,在那里你可以设置你的主类,**实际上是从 netbeans 默认帮助中得到的
回答by Chris Clark
As a comment, I had to allow a customer to execute a class in a jar which meant that the manifest file couldn't be modified (they couldn't be expected to do that). Thanks to the post by Anthony and samy-delux's comment, this is what the customer can now run to access the main of the specific class:
作为评论,我必须允许客户在 jar 中执行一个类,这意味着无法修改清单文件(不能期望他们这样做)。感谢 Anthony 的帖子和 samy-delux 的评论,这是客户现在可以运行以访问特定类的主要内容:
java -cp c:\path\to\jar\jarFile.jar com.utils.classpath -e -v textString
回答by Mohit Kapoor
Press F11 to Build and the Run the program. Once you run the program, you will have a list of classes. Select your main class from the list and click ok to run.
按 F11 生成并运行程序。运行程序后,您将获得一个类列表。从列表中选择您的主类,然后单击确定运行。
回答by Assem-Hafez
you can right click on the project select "set configuration" then "Customize", from there you can choose your main class.
您可以右键单击项目选择“设置配置”,然后选择“自定义”,从那里您可以选择您的主类。