Integration with Java

37 views
Skip to first unread message

Andy Hurd

unread,
Feb 16, 2011, 7:40:24 AM2/16/11
to Sleep Developers
Hello All,
I am working with jxl.jar file in Java that allows the manipulation
of Excel Files in Java. Does anyone have any examples on how to
incorporate an external library like that into a sleep script?

So I guess my questions are this:
1) How do you incorporate java into a sleep script?
2) How do you import external Java Libraries into a sleep script?

Any thoughts would be great,
Andy Hurd

Raphael Mudge

unread,
Feb 16, 2011, 9:09:00 AM2/16/11
to sleep-de...@googlegroups.com
Hi Andy,
I use Sleep quite a bit for this purpose.

Take a look at chapter 7 of the Sleep Manual:
http://sleep.dashnine.org/manual/hoes.html

The general idea is this:

import whatever.package.* from: filename.jar;

$object = [new ClassName: arg1, arg2, ...]; # instantiate the instance
$value = [$object method: arg1, arg2]; # call a method
$value = [$object method]; # call a method with no arguments.
$value = [ClassName someMethod: arg1, arg2]; # call a static method
$value = [ClassName fieldName]; # access a static field's value e.g.,
[Integer MAX_VALUE]
setField($object, var => 1); # object.var = 1;

Sleep guesses the Java values to change into and does a decent job of
this. If it's wrong for some reason, use the cast and casti functions
to cast a Sleep scalar into a specific Java type.

I usually start out by taking the hello world example for a library,
porting it to Sleep, and then hacking on it from there. Here's an
untested port of the first few lines from:
http://www.andykhan.com/jexcelapi/tutorial.html

import java.io.File;
import java.util.Date;
import jxl.* from: jxl.jar;

$workbook = [Workbook getWorkbook: [new File: "myfile.xls"]];
$sheet = [$workbook getSheet: 0];

$a1 = [$sheet getCell: 0, 0];
$b2 = [$sheet getCell: 1, 1];
$c2 = [$sheet getCell: 2, 1];

println([$a1 getContents]);
println([$b2 getContents]);
println([$c2 getContents]);

-- Raphael

> --
> -----
> To post to this group, send email to sleep-de...@googlegroups.com
> To unsubscribe from this group, send email to sleep-develope...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sleep-developers?hl=en
>
> The Sleep Scripting Project: http://sleep.dashnine.org/

Dan Fare

unread,
Feb 16, 2011, 9:13:39 AM2/16/11
to sleep-de...@googlegroups.com
Thanks for that response, I was wondering how that was done also.


Dan

ah...@nycap.rr.com

unread,
Feb 16, 2011, 9:46:21 AM2/16/11
to sleep-de...@googlegroups.com, Dan Fare
Raphael,
That is great. I have been using sleep for some financial contracts I have been working on. They want the process automated so putting them into sleep scripts has been wonderful. I was having issues with Java and its Excel access until I found the jxl library. The company didn't want to pay for the JExcel library and I don't blame them.

Your solution should make things easier.

Andy

Reply all
Reply to author
Forward
0 new messages