tags:

views:

1399

answers:

6

I have 2 class 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 uses different main function 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 clink on project >properties > run > select the class frm drop down list. So simple in netbeans. Netbeans rocks!

+10  A: 

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: http://stackoverflow.com/questions/602537/producing-executable-jar-in-netbeans

James Black
+1  A: 

You can set the Main-Class attribute in the jar file's manifest to point to which file you want to run automatically.

Spike Williams
A: 

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.

Rahul
+2  A: 

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.

Nate
+1  A: 

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.

erickson
A: 

public static void main( String[] args) is not found by n: NetBeans IDE 6.9 (Build 201006101454)

g bruno