0

I have created a .java file which used the .jar as the external library file by Eclipse. I only need a little public static function in the .java to run in R, maybe from a "jobject" call in R? does any one how to do it?

The closest solution is from https://stackoverflow.com/a/20291982/2842390:

library(rJava) .jinit(PATH_TO_YOUR_JAR) # this starts the JVM 

jobject <- .jnew("yourJavaClass")  ## call the constructor 

.jcall(jobject ,"I",method="YOUR_METHOD") ## call a method

but I cannot put my .java file in "yourJavaClass" as it results in Error in .jnew("hello3.java") : java.lang.ClassNotFoundException.

Your help is really appreciated!

  • Hi, welcome to stackoverflow, what the "R" means? You mean R language? then 2 minutes of searching ... `jar`- https://stackoverflow.com/questions/20291904/how-can-i-run-an-executable-jar-file-in-an-r-script and `java` - https://stackoverflow.com/questions/10319067/how-do-you-run-an-r-program-from-java Show some effort, - look at [How to ask](https://stackoverflow.com/help/how-to-ask), and [mcve] :) – xxxvodnikxxx Jan 07 '19 at 12:01
  • Possible duplicate of [How do you run an R program from Java?](https://stackoverflow.com/questions/10319067/how-do-you-run-an-r-program-from-java) – xxxvodnikxxx Jan 07 '19 at 12:02
  • Thank you! but my problem is I already have the .jar, now I need to run a specific function in a .java file but I couldn't find any similar situation. – user2842390 Jan 07 '19 at 13:07
  • Ah, then its not the same, but I dont think its a possible due the logical and security reasons https://stackoverflow.com/questions/7950538/how-to-call-method-in-jar-file-with-terminal jars can be imported to the project during development, and then you will get access to the public methods inside, but in this case, only thing which you can do, is to make some "libraries"- jars, and add them into program classpath during run if they will not be linked included in jar. But its not what you want. – xxxvodnikxxx Jan 07 '19 at 13:47

1 Answers1

0

You can call

.jaddClassPath('path/to/your/file.jar')

And then you can use any classes from that file.

Steves
  • 2,798
  • 21
  • 21