rJava NoSuchFileException when running jar from R - java

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?

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)

Related

How to provide the -Dlogback.configurationFile=./logback.xml option in nodejs spawn method

I have angular2-node.js application. I am executing a jar file through the node server.
Jar execution is happening fine but it's using the logback.xml present in the jar file.
Node js code:
app.get('/report/:parameter1/:parameter2', function(req, res) {
var fileName = path.join(__dirname, '..', 'javaFile', 'xyz.jar');
spawn('/usr/bin/java', ['-jar ', fileName, parameter1 , parameter2, '&'],{
stdio : ['ignore', out, err],
detached : true }).unref();
data = '{response: Success}';
res.status(200).json(data);
res.end();
});
I want to refer the different logback.xml file for jar execution while running the jar from UI. So, i tried the below code:
spawn('/usr/bin/java', ['-jar -Dlogback.configurationFile=./logback.xml', fileName, cacheName , cacheType, '&'],{
stdio : ['ignore', out, err],
detached : true }).unref();
But, it also didn't work and throw the below error:
Unrecognized option: -jar -Dlogback.configurationFile=./logback.xml
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
I am new to node js. I searched the web but couldn't get an answer.
Is there any way to provide the logback.xml file dynamically in node.js code something like we do in shell script like below:
nohup java -jar -Dlogback.configurationFile=./logback.xml xyz.jar
Can anyone provide any solution for this.
The args arguments is <string[]>, so you should split the multiple args into multiple elements of the array, like you've done for the other arguments. You can check the signature of the method here.
Try,
spawn('/usr/bin/java', ['-jar', '-Dlogback.configurationFile=./logback.xml'], ....

JPype (Python): importing folder of jar's

i am using JPype in order to work with java classes in python.
I have a folder that contains multiple self-written .jar files.
I know how to import multiple .jar's on the long way:
...
CLASSPATH = "/path/to/jars/first.jar:/path/to/jars/second.jar"
jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=%s" % CLASSPATH)
MYLIB= jpype.JPackage("org").mylib
MyClass = MYLIB.MyClass
myObj = MyClass()
This works fine, but i think there might be a better way.
I already tried this:
CLASSPATH = "/path/to/jars/*.jar"
and this:
CLASSPATH = "/path/to/jars/*"
In both cases following error occurs:
user#user:~/path/to/python/$ python test.py
Traceback (most recent call last):
File "test.py", line 23, in <module>
myObj = MyClass()
File "/usr/local/lib/python2.7/dist-packages/JPype1-0.6.2-py2.7-linux-x86_64.egg/jpype/_jpackage.py", line 60, in __call__
raise TypeError("Package {0} is not Callable".format(self.__name))
TypeError: Package org.mylib.MyClass is not Callable
My Question:
Is there any way to easily import a folder that contains multiple .jar's in JPype?
You can join the list of jar files with Python code without hardcoding
f'{str.join(":", ["path/to/jars/"+name for name in os.listdir("path/to/jars")])}'

Strange error when deleting directory from Java: 0 bytes, access denied

I have written method that recursively deletes directory with its contents.
Code is executed on Windows - Eclipse - JVM7
Here's the code:
/**
* Empty and delete a folder with recursion.
*
* #param folder
* folder to empty
*/
public static boolean rmdir(final String folderPath, boolean deleteParent) {
File folder = new File(folderPath);
// check if folder file is a real folder
if (folder.isDirectory()) {
File[] list = folder.listFiles();
if (list != null) {
for (int i = 0; i < list.length; i++) {
File tmpF = list[i];
if (tmpF.isDirectory())
rmdir(tmpF.toString(), true);
tmpF.delete();
}
}
if (deleteParent && !folder.delete()) {
return false;
} else
return true;
}
return false;
}
When the code is executed no error is thrown, saying that directory has been deleted successfully.
When I open drive to confirm deletion, I can still see the folder which is now 0 bytes in size with "Access denied" error when trying to open it.
EDIT:
I am administrator, have all permissions and can R/W to this folder from Windows explorer (before Java breaks it).
My question is has anyone seen this before and what could be the cause of this??
One more thing:
It seems that the directory gets set to "DeleteOnExit" instead of "Delete" right away.
Because when program ends in Lotus Notes or Eclipse directory disappears.
Ok I got this resolved...
I was not Java issue, it was Lotus Notes/Domino issue.
Here comes the long sentence:
It seems when Lotus Notes/Domino thread creates folder "A" and some files in it using LotusScript mkdir, rmdir, and other IO methods and then executes Java VM which tries to delete folder "A" with all files in it, parent thread (Lotus Notes/Domino) gets corrupted and loses directory structure handle which results in error above.
Solution to the problem is simple Dir$() statement after Java finishes folder deletion.
When Dir$() is executed, phantom folder disappears.
Probably because the Dir$() call refreshes and releases whatever should be refreshed and released internally.
Java is probably not the best language to solve this problem in. You can do this with batch file programming in one line: rmdir <directory-to-completely-remove> /s /q. If you absolutely must do this in Java, then you can run this command from a Java program on windows with: Runtime.getRuntime().exec("cmd /C start /min cmd.exe /K \"rmdir <directory-to-remove> /s /q & exit\"");

ProcessBuilder not executing Java class file properly

In a java file I am calling command line statement to execute another java file. This is what I am doing:
List<String> paramsExecute = new ArrayList<String>();
paramsExecute.add("java");
paramsExecute.add("-cp");
paramsExecute.add("input\programs\User_K Program1");
paramsExecute.add("1 2 3");
ProcessBuilder builderExecute = new ProcessBuilder(paramsExecute);
builderExecute.redirectOutput(new File(a.txt));
builderExecute.redirectError(new File(b.txt));
Execution of one of the Java files is producing b.txt as:
Error: Could not find or load main class 1 2 3
Another java file is producing b.txt as:
Usage: java [-options] class [args...] ...
But, when I am running these statements directly from the command line, it is executing correctly. The folder input\programs\ is in the same path as the src folder. The src folder contains the Java file containing the ProcessBuilder program. I have verified that the .class file is getting created correctly and in the right folder. I am running the program in windows.
Any help appreciated!!
This paramsExecute.add("input\programs\User_K Program1"); is been treated as a single command/parameter, saying that the class path should be equal to input\programs\User_K Program1
I think you want to use something more like...
paramsExecute.add("input\programs\User_K");
paramsExecute.add("Program1");
You should specify the classpath after '-cp' like
List<String> params = new ArrayList<String>();
params.add("java"); /* name of program to run, java */
params.add("-cp"); /* -cp */
params.add(System.getProperty("java.class.path")); /* class path information */
params.add("pkg.to.yourclass.ClassToRun"); /* full quailified class name */
params.add("1"); params.add("2"); params.add("3"); /* this is parameter to main */
"input\programs\User_K Program1" in your code is treated as a classpath information, not class to run because it follows '-cp', and "1 2 3" as a class name, not arguments passed to the main method.
It is not easy to retrieve classpath from the scatch.
If you want to create a process using an class located in the sample src folder, It is good to use System.getProperty("java.class.path"); to inherite classpath, or You should type the path info manually.

How do I load a LWUIT theme file into my Java project?

I am relatively new to Java, so bear with me.
I am completing a tutorial on LWUIT, and just want to load a simple theme, created using the editor. Here is the code in question:
try
{
Container container = c.getContainer();
container.setVisible(true);
Display.init(container);
Display.getInstance().setPureTouch(true);
//Resources r = Resources.open(getClass().getResourceAsStream("/res/Theme.res"));
Resources r = Resources.open("/res/Theme.res");
UIManager.getInstance().setThemeProps(r.getTheme("Simple"));
}
When I use the first (commented out) statement, I get
*** Signal: alarm { "name":"XletException", "domain":"ams", "appId":"com.thomasdge.xlet.hellojamaica.HelloJamaica", "msg":"XletAction['initXlet'] resulted in exception com.aicas.xlet.manager.AMSError: running xlet code caused java exception: initXlet() resulted in exception: java.lang.NullPointerException: <null>.read()I", "data":{ } }
When I use the other, I get
java.io.IOException: /res/Theme.res not found
I have my Theme.res file in /res/Theme. I have also tried it directly in the root, as well as /src. Same results for each.
Any ideas?
If you put the res file in that folder, you will need to go down one level. I recommend you to put the res in the src folder. So, /src/Theme.res. In the code you will only need to write Resources r = Resources.open("/Theme.res");
If resource file placed in res folder, you need to add res folder in project properties. Also you mentioned, the problem even in /src folder, I feel you didn't change the path. Just use Resources.open("/Theme.res") when you use /src folder. Also check the theme name. This should work.

Categories