how to set default main class in java?
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.
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?
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!
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
In the jar file you could just add this to your manifest.mft
Main-Class : A
The jar file would then be executable and would call the correct main.
On how to do this in Netbeans you can look at this: Producing executable jar in NetBeans
you can right click on the project select "set configuration" then "Customize", from there you can choose your main class.
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.
In each JAR file, the manifest will be a file with the following path / name inside the JAR - META-INF/MANIFEST.MF
There are ways to specify alternatively named files as a JAR's manifest using the JAR command-line parameters.
The specific class you want to use is specified using Main-Class: package.classname inside the META-INF/MANIFEST.MF file.
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.
Assuming your my.jar has a class1 and class2 with a main defined in each, you can just call java like this:
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 -classpath my.jar class1
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:
java -cp c:\path\to\jar\jarFile.jar com.utils.classpath -e -v textString
You can set the Main-Class attribute in the jar file's manifest to point to which file you want to run automatically.
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.
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.
In Netbeans 11(Gladle Project) follow these steps:
In the tab files>yourprojectname>
double click in the file "build.gladle"
than set in line "mainClassName:'yourpackagepath.YourMainClass'"
Hope this helps!
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.
you can easily run the Ant tasks from Netbeans.
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.
精彩评论