Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7771251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:36:19+00:00 2026-06-01T16:36:19+00:00

I am writing a Java wrapper library around a ruby gem so I am

  • 0

I am writing a Java wrapper library around a ruby gem so I am embedding ruby within Java and not the other way around. I seem to be in the vast minority!

If I have a ruby method that returns a Time object then I can very easily convert it into a java.util.Date object on the Java side like this:

public Date getStartTime() {
    IRubyObject result = RuntimeHelpers.invoke(runtime.getCurrentContext(),
        this, "start_time");
    return (Date) result.toJava(Date.class);
}

But I think I got lucky working this out by trial and error and not all like-seeming types can be converted in this way. I have another ruby method that returns a URI object (it could be a URI::HTTP or a URI::HTTPS in fact) but trying the obvious (given the above) conversion to a java.net.URI doesn’t work (I also tried it with java.net.URL):

public URI getUri() {
    IRubyObject result = RuntimeHelpers.invoke(runtime.getCurrentContext(),
        this, "uri");
    return (URI) result.toJava(URI.class);
}

This code compiles, but fails at run time:

Exception in thread "main" org.jruby.exceptions.RaiseException: (TypeError) cannot
convert instance of class org.jruby.RubyObject to class java.net.URI

I realise that in ruby a URI is actually a module and URI::HTTPS, etc are the classes, so I’m not entirely surprised that the above didn’t work. But there’s clearly a bit of internal “magic” being done for the Time/Date example so I was wondering if there were similar conversions provided for other types, which types and where they are documented.

Any pointers much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:36:20+00:00Added an answer on June 1, 2026 at 4:36 pm

    https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby says “Conversion of Types – Ruby to Java – See the JRuby rspec source code dir spec/java_integration for many more examples. [examples, …]”.

    Sure enough, coercion_spec.rb contains a lot of examples/specifications. Here’s the case you already use:

    describe "Time\"to_java" do
      describe "when passed java.util.Date" do
        it "coerces to java.util.Date" do
          t = Time.now
          d = t.to_java(java.util.Date)
          d.class.should == java.util.Date
        end
      end
    
      # [...]
    end
    

    I believe this is the best documentation currently available.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a library that wraps around a REST API. The wrapper I'm creating
I'm writing a Clojure wrapper for the Braintree Java library to provide a more
I'm writing wrapper for java library. Suppose ClassA and ClassB (in default package). In
Is there a C or C++ compatible library for reading and writing Java class
I am writing a driver to act as a wrapper around two separate MySQL
I'm writing a Java wrapper for c++ and would like to use a generic
I'm writing a wrapper in Java for a C++ program. The wrapper is done
I'm writing a terminal wrapper for a command-line program in Java, and I spawn
I'm writing a Java wrapper for an API and I have a fundamental question
I have writing Java code Using Jersey library to call Rest APIs. For my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.