1

I'm just trying to get a simple example of accessing a custom java class from within R using rJava.

HelloWorld.java

class HelloWorld {
        public static void main(String[] args){
                System.out.println("Hello World!");
        }
}

compiled .java to .class as such:

javac HelloWorld.java

R code (ran from same directory as HelloWorld.java and HelloWorld.class.

library(rJava)
> .jinit()
[1] 0
> .jnew("HelloWorld")
Error in .jnew("HelloWorld") : java.lang.ClassNotFoundException

Thank you for any pointers.

2
  • Why do you want pointers? Java uses references I thought... Commented Jun 19, 2012 at 17:27
  • hahaha, I see what you did there. Commented Jun 19, 2012 at 17:30

1 Answer 1

6

Since you're using a custom class you need to tell rJava where to find these custom classes. One way to do this is to specify the location of your classes when you call jinit.

library(rJava)
# Assuming HelloWorld is in the current working directory
.jinit(".")
.jnew("HelloWorld")

I would recommend reading the help page for .jinit

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This worked. I was going through many examples of code and none of them specified a path to the custom classes :/ Will read up on these functions a bit more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.