This discussion is archived
9 Replies Latest reply: Oct 22, 2007 7:50 AM by 807600 RSS

run Main class in ant task

807600 Newbie
Currently Being Moderated
Hi,
I have a simple ant task that runs the Hello.java correctly:
<target name="runHello" depends="compile" description="Run program">
         <java classname="com.myStuff.Hello">
         <arg line="Triona"/>
           <classpath>
             <pathelement location="${build}/classes"/>
           </classpath>
         </java>
          </target>
using the java class:
package com.myStuff;

public class Hello {
     public static void main(String[] args) {
          System.out.println("Hello....."+ args[0]) ;
     }
}
now, I'm trying to run a more complicated program called Main.java, that imports various other classes from the project and run it in ant like:
<target name="runMain" depends="compile" description="Run program">
         <java classname="com.myStuff.Main">
         <arg line="-cfg /simulatorss/simulator.cfg"/>
           <classpath>
             <pathelement location="${build}/classes"/>
           </classpath>
         </java>
</target>
The error I get back is:
[java] java.lang.NullPointerException
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:180)
[java] at org.apache.tools.ant.taskdefs.Java.run(Java.java:710)
[java] at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:178)
[java] at org.apache.tools.ant.taskdefs.Java.execute(Java.java:84)
[java] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
[java] at org.apache.tools.ant.Task.perform(Task.java:364)
[java] at org.apache.tools.ant.Target.execute(Target.java:341)
[java] at org.apache.tools.ant.Target.performTasks(Target.java:369)
[java] at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
[java] at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
[java] at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
[java] at org.eclipse.ant.internal.ui.antsupport.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
[java] at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
[java] at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.run(InternalAntRunner.java:423)
[java] at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.main(InternalAntRunner.java:137)
[java] Caused by: java.lang.NullPointerException
[java] at javax.swing.ImageIcon.<init>(Unknown Source)
[java] at com.myStuff.gui.Splash.<init>(Splash.java:32)
[java] at com.myStuff.Main.<init>(Main.java:623)
[java] at com.myStuff.Main.main(Main.java:1352)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
[java] at java.lang.reflect.Method.invoke(Unknown Source)
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.run(ExecuteJava.java:202)
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:134)

ant ideas?


Thanks,

Tri
  • 1. Re: run Main class in ant task
    807600 Newbie
    Currently Being Moderated
    I'm not sure what your problem is but I would make two changes -

    1) I would
    use
                <arg value="-cfg"/>
                <arg value="/simulatorss/simulator.cfg"/>
      
    instead of
    <arg line="-cfg /simulatorss/simulator.cfg"/>
         2) I would use fork="true" as an attribute of
    <java/>
    task.

    I suspect that "/simulatorss/simulator.cfg" represents a configuration file. Your program seems to think that the file is in the the file /simulatorss relative to your working directory but do you know which the working directory is? To start with you may need to be more explicit and specify the full path to the file.

    I note the double 's' in the directory specification /simulatorss/. Is this correct?
  • 2. Re: run Main class in ant task
    807600 Newbie
    Currently Being Moderated
    A bit of improvement but still get the error:
    Buildfile: C:\myProjects\runway_core\guitools\sims\build.xml
    init:
    [echo] C:/myProjects/javalibs
    check_config_beans:
    refresh_config_beans:
    compile:
    runClass:
    [java] Starting Simulator.
    [java] Exception in thread "main" java.lang.NullPointerException
    [java] at javax.swing.ImageIcon.<init>(Unknown Source)
    [java] at com.myStuff.gui.Splash.<init>(Splash.java:32)
    [java] at com.myStuff.Main.<init>(Main.java:623)
    [java] at com.myStuff.Main.main(Main.java:1352)
    [java] Java Result: 1
    BUILD SUCCESSFUL
    Total time: 3 seconds

    with your improvements - thanks!
    any ideas on the gui.Splash problem?

    Edited by: rooney28 on Oct 22, 2007 5:25 AM
  • 3. Re: run Main class in ant task
    807600 Newbie
    Currently Being Moderated
    any ideas on the gui.Splash problem?
    Since your class path only contains your classes directory, it looks to me like the splash image is accessed via the classpath and that you have not included it's directory in your classpath.
  • 4. Re: run Main class in ant task
    807600 Newbie
    Currently Being Moderated
    sabre150 wrote:
    2) I would use fork="true" as an attribute of
    <java/>
    task.
    That's usually a good idea, but if you don't fork it, you can run ant from a Java debugger, start a Java program, and still debug your program as it's in the same JVM, can't you? I can see this being valuable.
  • 5. Re: run Main class in ant task
    807600 Newbie
    Currently Being Moderated
    can you include 2 pathelements?

    I rebuilt the project from the cmd line and ran 'ant runMain' and got:


    Buildfile: C:\myProjects\runway_core\guitools\sims\build.xml
    init:
    [echo] C:/myProjects/javalibs
    check_config_beans:
    refresh_config_beans:
    compile:
    runMain:
    [java] Starting SITA Integration Platform Host Simulator.
    [java] Exception in thread "main" java.lang.NoClassDefFoundError: org/exolab/castor/xml/MarshalException
    [java] at com.myStuff.Main.<init>(Main.java:634)
    [java] at com.myStuff.Main.main(Main.java:1352)

    so I have to include these castor jars somehow
  • 6. Re: run Main class in ant task
    807600 Newbie
    Currently Being Moderated
    rooney28 wrote:
    can you include 2 pathelements?
    Look at
    http://ant.apache.org/manual/CoreTasks/java.html
    Especially the first example. In short, yes.
  • 7. Re: run Main class in ant task
    807600 Newbie
    Currently Being Moderated
    I'm sorry - i've tried everthing but still get the same error:
    Buildfile: build.xml

    init:
    [echo] C:/myProjects/javalibs

    check_config_beans:

    refresh_config_beans:

    compile:

    runShite:
    [java] Starting Simulator.
    [java] Exception in thread "main" java.lang.NullPointerException
    [java] at javax.swing.ImageIcon.<init>(ImageIcon.java:138)
    [java] at com.elandtech.sims.gui.Splash.<init>(Splash.java:32)
    [java] at com.myStuff.Main.<init>(Main.java:623)
    [java] at com.myStuff.Main.main(Main.java:1352)
    [java] Java Result: 1

    BUILD SUCCESSFUL
    Total time: 3 seconds

    with
    <target name="runMain" depends="compile" description="Run program">
        <java classname="com.myStuff.Main" fork="true">
         <arg value="-cfg"/>
         <arg value="/sims/sims.cfg"/>
         <classpath>
                <pathelement location="${build}/classes" />
                <pathelement location="${jar.name}" />
         </classpath>
        </java>
     </target>
  • 8. Re: run Main class in ant task
    807600 Newbie
    Currently Being Moderated
    still need help!!
  • 9. Re: run Main class in ant task
    807600 Newbie
    Currently Being Moderated
    What help can we now give! It is almost certainly a class path problem. We have no idea what your class path should be since we don't know what libraries you are using. We don't know where the image resources are that need to be loaded and we don't know how you are referencing them.