1

I am writing an ant task which uses below code :

public class Klazz extends Task{    
    public void execute() throws BuildException{

        HtmlUnitDriver driver = new HtmlUnitDriver();
        driver.get("file:///C:/sample/alltests-fails.html");
}

In eclipse the project named is "test-project" and used "libs" folder which contains the jars (ant.jar, selenium-server-standalone-2.44.0.jar) to be added in the classpath . If I run the code in Eclipse its working fine but while running as an ant task it throws java.lang.ExceptionInInitializerError . Below is the build.xml snippet to create the jar(named custom-task.jar) file which needs to be put in the %ant_home%\lib folder.

<target name="jar" depends="compile" >
        <mkdir dir="build/jar" />
        <jar destfile="${env.ANT_HOME}/lib/custom-task.jar">
            <fileset dir="build/classes" />         
            <restrict>
                <name name="**/*.class" />
                <archives> 
                    <zips>
                        <fileset dir="${basedir}/libs/" includes="**/*.jar" />
                    </zips>
                </archives>
            </restrict>
        </jar>
    </target>

May be the external jars/classes not added properly in the class path while creating the jar through the "jar" task above, resulting some missing class files causing the ExceptionInInitializerError. Advance thanks for any help on this .

below is the stack trace :

java.lang.ExceptionInInitializerError
        at org.cyberneko.html.HTMLScanner.scanEntityRef(HTMLScanner.java:1415)
        at org.cyberneko.html.HTMLScanner$ContentScanner.scan(HTMLScanner.java:2
059)
        at org.cyberneko.html.HTMLScanner.scanDocument(HTMLScanner.java:920)
        at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:499
)
        at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:452
)
        at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
        at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.pars
e(HTMLParser.java:926)
        at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:2
45)
        at com.gargoylesoftware.htmlunit.html.HTMLParser.parseHtml(HTMLParser.ja
va:191)


       at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(Defau
ltPageCreator.java:268)
        at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPa
geCreator.java:156)
        at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient
.java:455)
        at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:329)
        at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:394)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:4
77)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:4
66)

at mypkg.Klazz.execute(Klazz.java:15)

       at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
        at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.jav
a:106)
        at org.apache.tools.ant.Task.perform(Task.java:348)
        at org.apache.tools.ant.Target.execute(Target.java:435)
        at org.apache.tools.ant.Target.performTasks(Target.java:456)
        at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
        at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
        at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExe
cutor.java:41)
        at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
        at org.apache.tools.ant.Main.runBuild(Main.java:851)
        at org.apache.tools.ant.Main.startAnt(Main.java:235)
        at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
        at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
Caused by: java.lang.NullPointerException
        at java.util.Properties$LineReader.readLine(Properties.java:434)
        at java.util.Properties.load0(Properties.java:353)
        at java.util.Properties.load(Properties.java:341)
        at org.cyberneko.html.HTMLEntities.load0(HTMLEntities.java:101)
        at org.cyberneko.html.HTMLEntities.<clinit>(HTMLEntities.java:53)
        ... 33 more

Total time: 2 seconds
Som
  • 4,618
  • 4
  • 29
  • 32
  • @chrylis I have updated my original post with the stack-trace , please have a look . – Som Oct 28 '14 at 11:05
  • there needs to be, in the stacktrace a "caused by" part, which includes the Exception that triggers the ExceptionInInitializerError. Is this the whole trace? – Vlad Ilie Oct 28 '14 at 13:01
  • @Vlad Ilie I have updated the above snippet with that. Actualy its thrown by the line "driver.get()" line of the class named "Klazz.java" – Som Oct 28 '14 at 13:16
  • there is still no "caused by" part, is that omitted in your trace as well? ExceptionInInitializer happens only as an effect, and the cause is another exception. The type of Exception would be useful (it could be a simple NPE) – Vlad Ilie Oct 28 '14 at 13:29
  • @Vlad Ilie oh yes ... its an NPE .. I am not getting the bottom of the full stack trace , I have updated the trace . – Som Oct 28 '14 at 14:11

2 Answers2

1

Should there have been any class loading difficulties, I would assume a NoClassDefFoundError or a ClassNotFoundException would occur

The ExceptionInInitializerError is usually not what should draw attention, because it only says "Hey, programmer, an exception happened inside an initialization block"

More about initialization blocks here

Therefore, dealing with the NPE will fix the issue, but unfortunately I've no access to the code that could've caused this. Let me know and I shall edit the answer.

Vlad Ilie
  • 1,389
  • 1
  • 13
  • 37
1

@Vlad Ilie thanks for having a look , its solved now.. the problem is with jar creation ant script . The earlier ant task for the jar creation is not able to club all the jars in the class path and resulting... classnotfoundexception which in turn caused the ExceptionInInitializerError and NullPointerException .

Below is the fixed "jar" task which is successfully able to add all the jars in the class path .

<target name="jar" depends="compile">       
        <jar destfile="${env.ANT_HOME}/lib/custom-task.jar" basedir="build/classes" >           
            <zipgroupfileset dir="${basedir}/libs/" includes="*.jar"/>      
        </jar>
    </target>

Above I used zipgroupfileset which is very handy .

Som
  • 4,618
  • 4
  • 29
  • 32