Need exceptions in JRuby available in Java code that called it

I’ve implemented a DSL in JRuby that I’m making available from a Java
method.
My Java code has to run in Java 5 (cannot run in Java 6), so I started
out
using BSF. However, my JRuby exceptions were not making it up to BSF,
and
all my Java users could see were BSFException, with no usable content
about
what was wrong in their DSL script.

So, my first question is whether there is a way to get more information
in
Java about the exception that occurred in JRuby?

So, I’ve spent the last several hours trying to get JSR-223 to work in
Java
5, and I’m pretty convinced that something is wrong with the
distribution.
Can anyone verify that it works? When I try to compile my simple test
class, using either ScriptEngineManager or JRubyScriptEngineManager, I
get a
compile error that seems to indicate that jruby-engine.jar is compiled
with
the Java 6 compiler, making it useless for Java 5:

C:\sw\jsr223-engines\jruby>javac -cp
build/jruby-engine.jar;…/…/jruby/lib/jruby.jar;…/sjp-1_0-fr-ri/script-api.jar
Jsr223.java
Jsr223.java:5: cannot access
com.sun.script.jruby.JRubyScriptEngineManager
bad class file:
build\jruby-engine.jar(com/sun/script/jruby/JRubyScriptEngineManager.class)
class file has wrong version 50.0, should be 49.0
Please remove or make sure it appears in the correct subdirectory of the
classpath.
import com.sun.script.jruby.JRubyScriptEngineManager;
^
1 error

C:\sw\jsr223-engines\jruby>java -cp
.;build/jruby-engine.jar;…/…/jruby/lib/jruby.jar;…/sjp-1_0-fr-ri/script-api.jar
Jsr223
Exception in thread “main” java.lang.UnsupportedClassVersionError: Bad
version number in .class file

Am I doing something wrong?

Thanks,
Lee

View this message in context:
http://www.nabble.com/Need-exceptions-in-JRuby-available-in-Java-code-that-called-it-tp19522887p19522887.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

I’ve gotten past the Java 6 problem by building a jruby_engine.jar with
Java
5.

However, I still have not found a way to get anything from JRuby up to
the
Java ScriptException, which is essential.

Should I be able to raise a Java::ScriptException from within Ruby?

If I raise a Ruby exception with a string parameter, all I see in the
Java
ScriptException is org.jruby.exceptions.RaiseException. If I could just
get
the string up to Java, I’d be in good shape.

Is there any way to connect these dots?

Thanks,
Lee

nabblee wrote:

I’ve implemented a DSL in JRuby that I’m making available from a Java
method. My Java code has to run in Java 5 (cannot run in Java 6), so I
started out using BSF. However, my JRuby exceptions were not making it up
to BSF, and all my Java users could see were BSFException, with no usable
content about what was wrong in their DSL script.

So, my first question is whether there is a way to get more information in
Java about the exception that occurred in JRuby?


View this message in context:
http://www.nabble.com/Need-exceptions-in-JRuby-available-in-Java-code-that-called-it-tp19522887p19542304.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

I’ve drilled down pretty deeply into this issue, and I’m puzzled by the
nesting of the exceptions. It turns out that I can see the text I want
from
my JRuby raise statement if I take the ScriptException and call
se.getCause().printStackTrace(). That gives me:

MessageManipulationDSL.rb:80:in retrieve': Header hdr2X does not exist (HeaderNotFoundException) from MessageManipulationDSL.rb:190:in merge’

All I want is to include that “Header hdr2X…” string in my Java
exception.
However, I can’t seem to get at it programmatically! Although I am to
print
it using printStackTrace(), I can’t seem to get my hands on it using
APIs.
I need to throw a new exception, so I just want the String in the
Exception
constructor to be that “hdr2X” string. But nothing I’ve tried has been
able
to find it besides printStackTrace().

I tried digging through the source code to figure out where that string
comes from, but it’s just too unfamiliar, and I haven’t been able to get
my
bearings.

So, I’ve had to resort to printing the stacktrace to a string and
tokenizing
to get what I want (pretty kludgy).

              StringWriter sw = new StringWriter();
              PrintWriter pw = new PrintWriter(sw);
              se.getCause().printStackTrace(pw);
              pw.flush();
              pw.close();
              String errMsg = sw.toString();

Shouldn’t I be able to access that text in a cleaner way than this? Is
this
a failing of the JSR-223 code?

Thanks,
Lee

View this message in context:
http://www.nabble.com/Need-exceptions-in-JRuby-available-in-Java-code-that-called-it-tp19522887p19544161.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

You can add JSR-223 to Java 5 by adding the bits you can download at
https://scripting.dev.java.net.

Charles Oliver N.-2 wrote:

Java 5 does not support javax.script, does it? I’m confused what you’re
trying to do here.


View this message in context:
http://www.nabble.com/Need-exceptions-in-JRuby-available-in-Java-code-that-called-it-tp19522887p19553986.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Java 5 does not support javax.script, does it? I’m confused what you’re
trying to do here.

nabblee wrote:

the string up to Java, I’d be in good shape.

method. My Java code has to run in Java 5 (cannot run in Java 6), so I
started out using BSF. However, my JRuby exceptions were not making it up
to BSF, and all my Java users could see were BSFException, with no usable
content about what was wrong in their DSL script.

So, my first question is whether there is a way to get more information in
Java about the exception that occurred in JRuby?


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Hi Charles,

On Thu, Sep 18, 2008 at 6:36 AM, Charles Oliver N.
[email protected] wrote:

Java 5 does not support javax.script, does it? I’m confused what you’re
trying to do here.

javax.script API isn’t included in JDK 5 but is available to download
from The Java Community Process(SM) Program - JSRs: Java Specification Requests - detail JSR# 223, which works on JDK 5.
So, I put archives of JSR 223 JRuby engine compiled by JDK 5 at
https://scripting.dev.java.net/servlets/ProjectDocumentList?folderID=8848&expandFolder=8848&folderID=0,
which can be used even on JDK 5. This information is
http://wiki.jruby.org/wiki/Java_Integration#Java_6_.28using_JSR_223:_Scripting.29.

I think nabblee downloaded javax.script API’s archive, checked sources
of JRuby engine and compiled them.

-Yoko

Should I be able to raise a Java::ScriptException from within Ruby?

http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Hi nabblee,

When you use JSR 223 JRuby engine, you will get two types of
exceptions. One is raised by JRuby while Ruby script is executing, and
another is raised by JRuby engine after coming back to a Java program.
If you want to cook exceptions raised from JRuby, create a Writer type
object and set it to the context by using
javax.script.ScriptContext#setErrorWriter() method. Then, you can do
anything that Writer type object supports. However, this method
affects only for exceptions raised from JRuby. This is the policy of
the implementation so that users can cook exceptions raised in JRuby
engine whatever they want. It is easy because programs that use
javax.script API are Java programs, so uses can do it in a Java way.

On Wed, Sep 17, 2008 at 9:01 PM, nabblee [email protected] wrote:

I’ve drilled down pretty deeply into this issue, and I’m puzzled by the
nesting of the exceptions. It turns out that I can see the text I want from
my JRuby raise statement if I take the ScriptException and call
se.getCause().printStackTrace(). That gives me:

MessageManipulationDSL.rb:80:in retrieve': Header hdr2X does not exist (HeaderNotFoundException) from MessageManipulationDSL.rb:190:in merge’

This exception is raised from JRuby. Thus, you can do something to the
exception if you use javax.script.ScriptContext#setErrorWriter()
method.

So, I’ve had to resort to printing the stacktrace to a string and tokenizing
to get what I want (pretty kludgy).

                   StringWriter sw = new StringWriter();
                   PrintWriter pw = new PrintWriter(sw);
                   se.getCause().printStackTrace(pw);
                   pw.flush();
                   pw.close();
                   String errMsg = sw.toString();

You can do like this:

    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    SimpleScriptContext scriptContext = new SimpleScriptContext();
    scriptContext.setWriter(pw);
    ....
    engine.eval(reader, scriptContext);
    ....
    String errMsg = sw.toString();

Hope this helps.
-Yoko


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email