Stopwatch constructor needs URL scxmlDocument

145 views Asked by At

I have a problem. I wanted to add a stopwatch display to my app. I used this page as a reference. I downloaded and added necessary commons-scxml-0.9.jar libraries (with source code). There are no errors in Eclipse but when I debug it, in this class constructor of super-class (AbstractStateMachine) is called with this command:

super(StopWatch.class.getClassLoader().getResource("org/apache/commons/scxml/env/stopwatch.xml"));

but Super-class constructor doesn't get any attribute. It expects final URL scxmlDocument but only null appears.

I know how stopwatch.xml looks but where should I place it, and how do I create final URL scxmlDocument from it?

I tried everything but nothing worked.

Thank you all !!

This is stopwatch.xml, if i add it eclipse reports errors because of id attribute:

`<?xml version="1.0" ?> 

            <transition event="watch.start" target="running" /> 
    </state>
    <state id="running">
            <transition event="watch.split" target="paused" /> 
            <transition event="watch.stop" target="stopped" />
    </state>
    <state id="paused">
            <transition event="watch.unsplit" target="running" /> 
            <transition event="watch.stop" target="stopped" /> 
    </state>
    <state id="stopped">
            <transition event="watch.reset" target="reset" /> 
    </state>

`

1

There are 1 answers

0
mbmast On

Your code looks fine. If you're getting an exception at run time, you should have included it with your question. The XML also looks fine, except that you have excluded the first few lines and the last line (was this intentional or is your XML really missing these lines?). This is my XML:

<?xml version="1.0"?>
<!--
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
-->
<scxml xmlns="http://www.w3.org/2005/07/scxml"
       version="1.0"
       initial="reset">

    <state id="reset">
        <transition event="watch.start"   target="running"/>
    </state>

    <state id="running">
        <transition event="watch.split"   target="paused"/>
        <transition event="watch.stop"    target="stopped"/>
    </state>

    <state id="paused">
        <transition event="watch.unsplit" target="running"/>
        <transition event="watch.stop"    target="stopped"/>
    </state>

    <state id="stopped">
        <transition event="watch.reset"   target="reset"/>
    </state>

</scxml>

As for where to put the XML file, typically you would put it in the SRC directory. When Eclipse does the build, it won't know what to do with the XML file and so, its default behavior, is to copy it to the BIN directory where the compiled code goes. You may need to do a Project > Clean... to get this to happen. This is where the run time environment typically expects to find resources like this.

Here's a really good trick: If you're doing your development on any version of Windows you can go to Microsoft's incredibly good Sysinternals site (http://technet.microsoft.com/en-US/sysinternals). Here you can find the wonderful Process Monitor utility. Download, install and run this free utility. From within the utility, create a Process Monitor Filter: Path | contains | stopwatch.xml Make sure this filter is enabled and all the other (predefined) filters are disabled.

Then run your compiled stopwatch program. The utility will display EVERY location the application (and all of the class loaders contained therein) looks for stopwatch.xml.

You can then move/copy/whatever the file to make sure it's in a place the application looks to locate the file. This utility is especially handy for similar "I can't find the resource" issues that occur when writing J2EE apps (e.g. Tomcat, Websphere, etc.).

And finally, while you didn't mention anything about dependent jar files, make sure these are on the classpath:

commons-beanutils-1.9.2.jar
commons-digester-2.1.jar
commons-jexl-1.1.jar
commons-logging-1.2.jar
commons-scxml-0.9.jar
log4j-1.2.17.jar
xalan.jar