0

I've currently got an eclipse project with two main methods and was wondering if it's possible to create a jar file which runs an Apache Ant script when the jar file is executed. I've provided an Ant script below where two main methods are run. Thanks in advance.

    <?xml version="1.0"?>
      <project name="Test" default="run_external">
        <target name="compile">
            <delete dir="bin" />
            <mkdir dir="bin" />
            <javac srcdir="src" destdir="bin" />
        </target>
        <target name="run_external" depends="compile">
            <parallel>
                <exec executable="cmd" dir="bin">
                    <arg value="/c start cmd.exe /k java test.Main" />
                </exec>
                <exec executable="cmd" dir="bin">
                    <arg value="/c start cmd.exe /k java test.Main2" />
                </exec>
            </parallel>
        </target>
    </project>
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
user1087943
  • 489
  • 1
  • 7
  • 15

2 Answers2

0

Why would you need Ant? From the comment I deduce that you want your Java program to launch another one. If so, simply use:

Runtime.getRuntime().exec("java -jar otherprogram.jar");

or whatever the command line is. Please note that there are more advanced versions of Runtime.getRuntime().exec(), so have a look at the javadoc.

EDIT: from Runtime.getRuntime().exec() docs:

Executes the specified string command in a separate process.

So you are spawning another indipendent JVM.

Don't use directly java ..., instead compose the command using environment variables: this is only for demostrating the function.

Anyway, if you need to do this at startup, I suggest you use a shell script launching the three programs.

Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
  • Apologies for the confusion. I'm looking for all the java programs to launch independently from each other on separate JVMs and consoles. Therefore that's why I used an Ant script. I'd like for when the Jar file is run, the ant script is executed which would spawn multiple other command lines with the specific jar files running on them. – user1087943 May 06 '13 at 15:24
  • If you use `Runtime.exec()` a different process (and, so, JVM) is spawned. Check edit above. – Stefano Sanfilippo May 06 '13 at 15:27
0

Create a jar using ant

 <target name="jar" depends="compile">
    <mkdir dir="${jar.dir}"/>
    <jar destfile="${jar.dir}/helloworld.jar" basedir="${classes.dir}"/>
 </target>

When you include multiple main classes in one jar file, then each must be invoked using the -cp flag and with the fully qualified name of the main class specified.

java -cp helloworld.jar com.test.Main1 && java -cp helloworld.jar com.test.Main2

This will give you an output (I just printed the class names)

I am main 1
I am main 2

Alternatively Based on your requirement you can create single entry point ( I will prefer this), I mean a single class with main method and then call the other main methods from this class

example

create a class

public class Main {

public static void main(String[] args) {

    Main1.main(args);
    Main2.main(args);
   }
 }

Include this main class in Manifest.MF

<target name="jar" depends="compile">
    <mkdir dir="${jar.dir}"/>
     <jar destfile="${jar.dir}/helloworld.jar" basedir="${classes.dir}">
        <manifest>
            <attribute name="Main-Class" value="com.test.Main"/>
        </manifest>
    </jar>
</target>

And execute

   java -jar helloworld.jar

Output of this

 I am main 1
 I am main 2
NullPointerException
  • 3,732
  • 5
  • 28
  • 62