Reputation: 1129
I'm trying to run a runnable JAR file from R, using the rJava package. This jar has to write and read some files to/from external folders, that are in the same path as the jar file itself, like this:
mypath/myjar.jar
mypath/folder1
mypath/folder2
mypath/input_file1.txt
mypath/input_file2.txt
The program works fine if I call it by opening a console in "mypath" and running the jar the following way:
java -jar myjar.jar input_file1.txt input_file2.txt false
But when I try to run this code in R, using rJava, the code crashes at some point, because it can't find neither mypath/folder1 nor mypath/folder2, even though the working directory is correctly defined as "mypath".
jinit(".",force.init=TRUE) # this starts the JVM
.jaddClassPath("myjar.jar")
jobject <- .jnew("package_name/Main") ## call the constructor
result_java <- rJava::.jcall(obj = jobject, returnSig = "V", method = "main", c("input_file1.txt","input_file2.txt","false"))
In fact, the java program is called, it is able to actually find the input files which are also in mypath, but for some reason crashes when it tries to write to folders in mypath (such as folder1 and folder2) with the error:
Error executing task java.nio.file.NoSuchFileException: folder1/some_file.txt
I really have no idea what's going on, spent hours on this. Am I missing something really obvious here?
Upvotes: 1
Views: 297
Reputation: 13425
When you run your code using Java, you are inside mypath
and locations folder1
and folder2
are visible to your code.
Maybe, you should pass (as argument) location of directory, and instead of accessing folder1
in your Java code, you should access explicit path.
result_java <-
rJava::.jcall(
obj = jobject,
returnSig = "V",
method = "main",
c(
"input_file1.txt",
"input_file2.txt",
"false",
"full_path_to_your_mypath_location"))
Then, inside main
, you could simply open full_path_to_your_mypath_location/some_file.txt
. When you start R, you probably no longer inside directory with your code. You can also try to change dir
setwd(full_path_to_your_mypath_location)
Upvotes: 0