Using Java exec to Run a jar from JSP with JRI - java

I got a task to run R script in java, so I used the JRI library for this case, and I need call it from jsp, so I write a page simply call by
Runtime.getRuntime().exec("java -Djava.library.path=/home/XXX/R/x86_64-pc-linux-gnu-library/2.15/rJava/jri/ -cp /home/XXX/R/x86_64-pc-linux-gnu-library/2.15/rJava/jri -jar /home/XXX/MyJar.jar")
This method -> re.waitForR() always return false
Rengine re = new Rengine(new String[] { "--vanilla" }, false, null);
if (!re.waitForR()) {
System.out.println("Cannot load R");
return;
}
But when I run with console
java -Djava.library.path=/home/XXX/R/x86_64-pc-linux-gnu-library/2.15/rJava/jri/ -cp /home/XXX/R/x86_64-pc-linux-gnu-library/2.15/rJava/jri -jar /home/XXX/MyJar.jar
It is good and no error happened!
How can I do?
PS. My Jsp runs on Jetty.

Related

Issue executing a java compiled file from java source

I am trying to execute a java compiled file from another java program, and I am having some issues.
When I run from my terminal the command java -cp ".:lib/MyLib.jar" javaFiles/g1/MyCompiledProgram I can execute MyCompiledProgram without any issue.
But when I try to execute the same command from code using the following method:
Process process = Runtime.getRuntime().exec(String.format("java -cp \".:%s\" javaFiles/g1/MyCompiledProgram",Path.of(PATH_MYLIB)));
String error = null;
process.waitFor();
if(process.exitValue() != 0){
try(Scanner scanner = new Scanner(process.getErrorStream())){
error = scanner.useDelimiter("\\A").next();
}
System.out.println(error);
}
I get a ClassNotFoundException error. I checked that the directory java was using to execute the command was correct (running the pwd command) and it is the correct one.
Does anyone has any idea why is not finding the class? Thanks :)

How to run JAR file using php script?

I tried JAVA bridge to get output from my jar file followed this https://stackoverflow.com/a/10253798/1870048, I am using XAMP Server and from that Apache and Tomcat server is been used
require_once("http://localhost:8080/JavaBridge/java/Java.inc");
$cartObj = Java("Cart"); //Javav("Cart");
$cartObj->Cart("098765");
$cartObj->setLang("EN");
echo $cartObj->getLang();
but gives error :
Fatal error: Call to undefined function Javav()
What should I exactly I look into?
Also I tried below way also to call the jar file by placing the jar file in my zampp htdoc folder: (D:\xampp\htdocs\jar)
// Using shell_exec
exec('java -jar AutoDiscounts.jar '.$jsonRequest.' 2>&1', $result);
print_r($result);
// Using shell_exec
$arg1 = $jsonRequest;//"My_INPUT_PARAMETER";
$output = '';
$output =shell_exec("java -jar AutoDiscounts.jar $arg1");
echo "Done calling shell exec:";
echo $output;
But both above cases does not give any output .Basically my jar will do some cart processing and return output and it works in the JDevloper

nodejs service -in windows system ,unable to execute jar files

I have Nodejs - server.js code in my windows system from which i want to execute a simple jar file.
So i used follwoing code :
cmd = "java -jar C:\\libs\\sample.jar";
var Exec =exec(cmd, function(error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
}//end of if
});//end of exec cmd
When i run the "node server.js" in command prompt ,jar is executing perfectly.
After this i made this "node server.js" command as service by referring following link :
https://github.com/coreybutler/node-windows
i.e node-windows,But now once its become service i am unable to exectue simple.jar,even i am enble to run simple "java -version" command.
Except java all other command are working fine even after i made nodejs as service.
Please help me out.....
New Update : After testing lot of different cases, Finally i came to know that jars containing JFrame components is not opening...how to solve this

Executing python compiled script (.pyc) in Java

I have a python compiled script (script.pyc , I haven't the .py file)that work well from my windows command prompt, and I want to execute it from my Java's application.
I tried to use runtime() method :
Runtime runtime = Runtime.getRuntime();
runtime.exec(new String[] {"C:\\toto\\tools\\script.pyc" ,"arg","arg2" });
but I get an error :
Exception in thread "main" java.io.IOException: Cannot run program "C:\Nuance\VoCon Hybrid\SDK_v4_3\tools\clctodict.pyc": CreateProcess error=193, %1 n?est pas une application Win32 valid
The script work well in my terminal ("arg" is a txt file, "arg2" is the output name, and the script does its job without any problem).
I also try to launch my script with getDesktop() :
File fie = new File("C:\\toto\\tools\\script.pyc" ,"arg","arg2");
Desktop.getDesktop().open(fie);
There is no problem, but I can't add argument, so I can just see a terminal windows opening during a few second before disappearing instantly.
I have also tried to use JPython, without success too (maybe we can't use methode "execfile" on a .pyc????)
You can do something like
Process p = Runtime.getRuntime().exec(new String[]{"python.exe" ... other args)
Then you can invoke p.waitFor() to wait for the end of the process and p.exitValue() to test if the program exited successfully.
You can also get the output stream via p.getOutputStream() to retrieve the text printed by your python script
Please refer to the class documentation for further information : http://docs.oracle.com/javase/6/docs/api/java/lang/Process.html
Just like you need a jvm to run a .class, you need a python interpreter to run a .pyc.
Try something like:
runtime.exec(new String[] {"c:\\Python26\\bin\\python.exe", "C:\\toto\\tools\\script.pyc" ,"arg","arg2" });

Launching another java application fails from java web start

As, it is easy to download jars and other artifacts using
http://code.google.com/p/jnlpdownloader/
So, I am trying to execute my business application from a small launcher application(this launcher application is running as Java web start application).
This launcher application,
first download executable business application jar from URL(available at runtime only)
And then execute the executable business jar.
But, the following code doesn't seems to work, nor, it is throwing any exceptions
String[] command = {"java -jar", "JarFromURL.jar"};
Runtime r = Runtime.getRuntime();
try {
r.exec(command);
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "Voilla...") ;
}
The array of parameters passed to exec() should be split this way.
String[] command = {"java", "-jar", "JarFromURL.jar"};

Categories

Resources