Questions about Class Loading

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Questions about Class Loading

Kluck.Dennis
Questions about Class Loading

Hi,
I am trying to run a script from a java class. When I run the script I get the following error:
        Typed variable declaration : Class: SimpleDateFormat not found in namespace

My questions is, how do I get the class loader into the interpreter so I can create any of the base java objects?
If I comment out where it is using the SimpleDateFormat, I get an error trying to create one of my classes that is in the same package as the class I am calling the script from. So how do I get the class loader information into the script?

My Java code looks like this:

private List doCalculation(Hashtable allData, Date startDate, Date endDate) {          
        List returnData = new ArrayList();
        Interpreter interpreter = new Interpreter();

// I have tried messing with class loaders but still not working
//      interpreter.setClassLoader(ClassLoader.getSystemClassLoader());
// Or I tried this?
//      BshClassManager bshCM = new BshClassManager();
//      bshCM.setClassLoader(ClassLoader.getSystemClassLoader());
//      NameSpace ns = new NameSpace(bshCM, "com.test.this");
//      interpreter.setNameSpace(ns);
       
       
        try {
                interpreter.set("startDate", startDate);
                interpreter.set("endDate", endDate);
                interpreter.set("allData", allData);
               
                interpreter.source("LocationOfMyScript.bsh");
                       
                returnData = (List) interpreter.get("calculatedData");

                } catch (EvalError e) {
                        e.printStackTrace();
                } catch (Exception e) {
                        e.printStackTrace();
                }
       
        return returnData;
}      



My Script looks like this:
String getValueForDate(List fromThisCollection, Date thisDate){
                SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
               
                for (Iterator iter = fromThisCollection.iterator(); iter.hasNext();) {
                        GenericDataInterface data = (GenericDataInterface) iter.next();
                        if (sdf.format(data.getStartDate()).equals(sdf.format(thisDate)) ){
                                // we found the matching data
                                return data.getValue();
                        }
                }
                // if we get here then we didn't find a matching date
                return "0";
}// end function


// I need to loop the dates so I can perform the calculation

//              I also tried messing with the namespace here but it then gives me
//                      an error about my passed in "startDate" value is not reconginized
//              setNameSpace( this.caller.namespace );
               
                Calendar calStart = Calendar.getInstance();
                calStart.setTime(startDate);

                List grossData = (List) allData.get("Gross");
                List auxData = (List) allData.get("Aux");

                Date tempDate = startDate;
                SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
                List calculatedData = new ArrayList();
               
                while (tempDate.before(endDate) || tempDate.equals(endDate)) {
                        System.out.println("TempDate=" + sdf.format(tempDate));
                        String grossStringValue = getValueForDate(grossData,tempDate);
                        String auxStringValue = getValueForDate(auxData,tempDate);
                        try {
                               
                                long grossValue = Math.round(Double.parseDouble(grossStringValue));
                                long auxValue = Math.round(Double.parseDouble(auxStringValue));
                                long netValue = grossValue - auxValue;
                               
                                GenericDataInterface genericData = new GenericData();
                                genericData.setDataName(this.name);
                                genericData.setStartDate(tempDate);
                                genericData.setValue(String.valueOf(netValue));
                                calculatedData.add(genericData);
                               
                        } catch (Exception e) {
                                System.out.println(e);
                        }
                       
                        calStart.add(Calendar.MONTH, 1);
                        tempDate = calStart.getTime();
                }

Any clue as to what I am doing wrong?

Thanks for taking the time to look at this,
Dennis

Reply | Threaded
Open this post in threaded view
|

Re: Questions about Class Loading

Wade Chandler-6
--- "Kluck.Dennis" <[hidden email]>
wrote:

> Hi,
> I am trying to run a script from a java class. When
> I run the script I
> get the following error:
> Typed variable declaration : Class:
> SimpleDateFormat not found
> in namespace
>
> My questions is, how do I get the class loader into
> the interpreter so I
> can create any of the base java objects?
> If I comment out where it is using the
> SimpleDateFormat, I get an error
> trying to create one of my classes that is in the
> same package as the
> class I am calling the script from. So how do I get
> the class loader
> information into the script?
>
> My Java code looks like this:
>
> private List doCalculation(Hashtable allData, Date
> startDate, Date
> endDate) {
> List returnData = new ArrayList();
>     Interpreter interpreter = new Interpreter();
>
> // I have tried messing with class loaders but still
> not working
> //  
>
interpreter.setClassLoader(ClassLoader.getSystemClassLoader());
> // Or I tried this?
> //   BshClassManager bshCM = new BshClassManager();
> //  
>
bshCM.setClassLoader(ClassLoader.getSystemClassLoader());

> //   NameSpace ns = new NameSpace(bshCM,
> "com.test.this");
> //   interpreter.setNameSpace(ns);
>    
>    
>     try {
>     interpreter.set("startDate", startDate);
>     interpreter.set("endDate", endDate);
>     interpreter.set("allData", allData);
>    
>     interpreter.source("LocationOfMyScript.bsh");
>
> returnData = (List)
> interpreter.get("calculatedData");
>
> } catch (EvalError e) {
> e.printStackTrace();
> } catch (Exception e) {
> e.printStackTrace();
> }
>    
>     return returnData;
> }
>
>
>
> My Script looks like this:
> String getValueForDate(List fromThisCollection, Date
> thisDate){
> SimpleDateFormat sdf = new
> SimpleDateFormat("MM/dd/yyyy");
>
> for (Iterator iter =
> fromThisCollection.iterator();
> iter.hasNext();) {
> GenericDataInterface data =
> (GenericDataInterface) iter.next();
> if
>
(sdf.format(data.getStartDate()).equals(sdf.format(thisDate))

> ){
> // we found the matching data
> return data.getValue();
> }
> }
> // if we get here then we didn't find a matching
> date
> return "0";
> }// end function
>
>
> // I need to loop the dates so I can perform the
> calculation
>
> // I also tried messing with the namespace here but
> it then
> gives me
> // an error about my passed in "startDate" value
> is
> not reconginized
> // setNameSpace( this.caller.namespace );
>
> Calendar calStart = Calendar.getInstance();
> calStart.setTime(startDate);
>
> List grossData = (List) allData.get("Gross");
> List auxData = (List) allData.get("Aux");
>
> Date tempDate = startDate;
> SimpleDateFormat sdf = new
> SimpleDateFormat("MM/dd/yyyy");
> List calculatedData = new ArrayList();
>
> while (tempDate.before(endDate) ||
> tempDate.equals(endDate)) {
> System.out.println("TempDate=" +
> sdf.format(tempDate));
> String grossStringValue =
> getValueForDate(grossData,tempDate);
> String auxStringValue =
> getValueForDate(auxData,tempDate);
> try {
>
> long grossValue =
> Math.round(Double.parseDouble(grossStringValue));
> long auxValue =
> Math.round(Double.parseDouble(auxStringValue));
> long netValue = grossValue - auxValue;
>
> GenericDataInterface genericData = new
> GenericData();
> genericData.setDataName(this.name);
> genericData.setStartDate(tempDate);
>
> genericData.setValue(String.valueOf(netValue));
> calculatedData.add(genericData);
>
> } catch (Exception e) {
> System.out.println(e);
> }
>
> calStart.add(Calendar.MONTH, 1);
> tempDate = calStart.getTime();
> }
>
> Any clue as to what I am doing wrong?
>
> Thanks for taking the time to look at this,
> Dennis
>
>

Have you used the correct import statements in your
script?

Wade


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users