How to start rserve automatically from java in windows - java

I have created a java application in eclipse. The application used Rserve package to connect to R and run r scripts. Before running my application, i have to start rserve from within Rstudio like this:
library(Rserve)
Rserve()
This Java code would be bundled as an executable file, so is there a way that Rserve() is invoked automatically(in windows) as soon as the code is run so that I can skip this manual step of starting Rserve using through RStudio?

The https://github.com/yannrichet/rsession project achieves exactly that for you.
Though it might be interesting to have a look at this: https://github.com/subes/invesdwin-context-r
As it integrates RSession and keeps a pool of Rserve connections for performance reasons without you having to do much for it. You can also switch to other runtime solutions like JRI, RCaller, Renjin without having to change your script code.

I'm not sure if there's a cleaner way to do this, but the way I've solved this is by starting it up console style from within my java program. For this to work, you have to put the path to the R executables in your systems path:
public Process rserve = null;
public static void startRServer() throws InterruptedException, IOException {
// check the runtime environment to see if there's an active Rserve running
String existingRserve = "";
try {
Process p = Runtime.getRuntime().exec("tasklist /FI \"IMAGENAME eq Rserve.exe\"");
p.waitFor();
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
existingRserve = in.readLine();
} catch(IOException e){}
if(rserve == null || existingRserve.contains("No tasks are running")) {
// start and rserve if we don't have one for this run yet,
// or if it has unexpectedly failed since we last used it
try {
rserve = Runtime.getRuntime().exec("Rscript -e \"library(Rserve); Rserve()\"");
rserve.waitFor();
} catch (IOException e) {
System.out.print("*** R Error: Unable to start the R server ***");
}
}
}

Related

Read from already open CMD - java

Is there anyway that a java application can read from an already open CMD window. For example, I've opened a cmd, ran "dir" and then decided to run my application, is there any way my application can read the information that's already in the open CMD window without opening another?
Thanks in advance
UPDATE:
My hope was that when one java application was ran, it would check any currently opened CMD windows to ensure that another one of my command-line applications hadn't been ran before it opened it itself.
Just spool the output of the executed command in a file.
For example : do dir > spool.txt
and from the java program open the spool.txt file with a FileInputStream.
To be sure that the content is totally written before reading it, you could for example :
use a marker in the written file to indicate it
check the modification date of the written file (if the modification date doesn't change during a defined period, it may be the signal that the writing is finished)
Update with updated question : checking whether a CMD windows is opened
UPDATE: My hope was that when one java application was ran, it would
check any currently opened CMD windows to ensure that another one of
my command-line applications hadn't been ran before it opened it
itself.
You could simply list the running processes of Windows by using the tasklist.exe program.
The information is retrieved with one line by process.
Then, check if one of the lines starts with cmd.exe.
If it is the case, it means a cmd.exe program is already running.
Here is a sample code ;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CmdProcessSearch {
public static void main(String[] args) throws IOException {
boolean anyCmdProgramOpened = isAnyCmdProgramOpened();
System.out.println("isOpened = " + anyCmdProgramOpened);
}
public static boolean isAnyCmdProgramOpened() {
Process p;
try {
p = Runtime.getRuntime().exec(System.getenv("windir") + "\\system32\\" + "tasklist.exe");
String line;
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
if (line.startsWith("cmd.exe")) {
return true;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
The C:\windows\system32 is usually in the PATH environment variable.
So, p = Runtime.getRuntime().exec("tasklist.exe"); should be enough
but to prevent any side effect or misconfiguration problem, specifying the absolute path is better.
New update with new comment : checking whether the Java application is running
I have used this code for a different section but if there is a cmd
window running, there is no guarantee that the user has used it to run
my program, package process;
In this case, if you can install jdk tools on the client machine you could check if is running a Java process using as main class, the main class of your application.
You could use jps (Java Virtual Machine Process Status Tool).
Here is the available options :
OPTIONS
The jps command supports a number of options that modify the
output of the command. These options are subject to change or removal
in the future.
-q
Suppress the output of the class name, JAR file name, and arguments passed to the main method, producing only a list of local VM
identifiers.
-m
Output the arguments passed to the main method. The output may be null for embedded JVMs.
-l
Output the full package name for the application's main class or the full path name to the application's JAR file.
-v
Output the arguments passed to the JVM.
-V
Output the arguments passed to the JVM through the flags file (the .hotspotrc file or the file specified by the -XX:Flags=
argument).
-Joption
Pass option to the java launcher called by jps. For example, -J-Xms48m sets the startup memory to 48 megabytes. It is a common convention for -J to pass options to the underlying VM executing
applications written in Java.
jps -l command that outputs the full package name for the application's main class or the full path name to the application's JAR file.
Here is a sample code by searching the main class of the application process.JavaProcessSearch :
package process;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class JavaProcessSearch {
public static void main(String[] args) throws IOException {
boolean isProcessRunning = isAJavaProcessWithMainClass("process.JavaProcessSearch");
System.out.println(isProcessRunning);
}
public static boolean isAJavaProcessWithMainClass(String mainClass) {
Process p;
try {
String javaHome = System.getenv("JAVA_HOME");
p = Runtime.getRuntime().exec(javaHome + File.separator + "bin/jps -l");
String line;
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
if (line.contains(mainClass)) {
return true;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
If you cannot install jdk tools, you could store "somewhere" on the client machine, the process id of the application when it is started.
You can retrieve this information with the following method : ManagementFactory.getRuntimeMXBean().getName().
When it is not any longer running (stopped or crashed), you could remove this process id information.
When the application ia started, if a process id is retrieved on the client machine, you don't allow the application to start.
For stopped application case, shutdown hook should do the job.
For crashed application case, you could use a deamon thread that checks on a regular basis whether the application is always running by relying on the stored process id.

Set focus of any application with Java (OSX)?

How can I set the focus (e.g. cmd+tab) of an arbitrary application (Java or not) from a Java program, on OSX?
Looking for an answer to this question, I came across this question, but it doesn't really help for OSX.
EDIT: one possibiltiy seems to be to use something like Quicksilver, and a Robot to send it keypresses with modifiers. I'd prefer something more portable, though, that requires less setup to make changes after it's compiled....
You should be able to reactivate an already running app using the open command that comes with OS X:
Runtime.exec("open /path/to/Whichever.app");
(Or some equivalent overload of that function.) This will also open an app if it's not running yet.
Chuck's answer tipped me off to osascript, so I decided to give it a shot straight from the command line. Managed to get it working with Runtime.exec(), osascript, and AppleScript.
Java launches an AppleScript and passes it the application name, using osascript from the command line, via Runtime.exec():
try {
List<String> shellCommandList = new ArrayList<String>();
shellCommandList.add("osascript");
shellCommandList.add("activateApplication.scpt");
shellCommandList.add(appName);
String[] shellCommand = (String[])shellCommandList.toArray(new String[0]);
Process p = Runtime.getRuntime().exec(shellCommand);
// if desired, pipe out the script's output
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String cmdOutStr = "";
while ((cmdOutStr = in.readLine()) != null) {
System.out.println(cmdOutStr);
}
// if desired, check the script's exit value
int exitValue = p.waitFor();
if (exitValue != 0) {
// TODO: error dialog
System.err.println("Invalid application name: "+ appName);
}
} catch (Exception e) {
e.printStackTrace();
}
And the AppleScript uses a run handler to capture the incoming argument:
on run (arguments)
set appName to (item 1 of arguments)
tell application appName to activate
return 0
end run
You can use the javax.script API to run AppleScripts. So you can write a script along the lines of "tell application "WhateverApp" to activate", filling in your arbitrary application for WhateverApp, and it should do what you want.

Running UNIX Source command in Java

This is my first question on stackoverflow so I'll try to keep it concise and relevant.
I'm currently creating a Java program that is attempting to call an external program located on the system, in order to do this however I am required to call a shell script that sets up links to the relevant libraries to ensure that the system is linked to these before the external program can be executed.
The issue at hand is that I cannot invoke the shell script through Java, I've researched high and low and am aware that of alternative ways such as the use of the ProcessBuilder class. Unfortunately I'm quite new to the world of trying to invoke command line statements through Java and so I'm stuck for answers.
An example of the code I am using can be found below:
private void analyse_JButtonActionPerformed(java.awt.event.ActionEvent evt) {
// Get project path for copying of Fortran program to folder and execution
String projectPath = Newproject_GUI.getProjectPath();
String sourcePath [] = {"/bin/sh ", "-c ","source ~/set_env_WRF_gnu.sh"} ;
Runtime fortranAnalyser = Runtime.getRuntime();
try {
Process p = fortranAnalyser.exec("cp main.exe " + projectPath);
Process k = fortranAnalyser.exec(sourcePath);
BufferedReader is = new BufferedReader(new InputStreamReader(k.getInputStream()));
String line;
while ((line = is.readLine()) != null) {
System.out.println(line);
}
} catch (IOException ex) {
Logger.getLogger(Analyser_GUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
Process p works fine and does indeed copy main.exe to the intended directory when the method is called. Process k however does not and this is where the issue is.
Thanks in advance.
The issue is "source" is internal command of BASH (you are using "sh" but that is just BASH in the simplified mode). So what you do is:
you spawn new process "sh" and source something there (setting some VARIABLES I guess)
the process ends and all VARIABLES are lost
you spawn another process, but VARIABLES are already gone
I am not sure if you use those variables later on, but according to the script name it is probably setting some. Don't do that like this.
By the way if you only want to execute script in bash, you don't need to source it. To get it's side effects, just execute it with:
String sourcePath [] = {"/bin/sh ", "/home/XYZ/set_env_WRF_gnu.sh"} ;
Please note you cannot use ~ in this case, use Java to get your home dir.

unable to run ksh script from java: java.io.IOException: .: not found

I tried to run a shell script from java code, but I am facing problem. The script is in batchstart.sh file -
#!/bin/ksh
export DISPLAY=:0.0
Now the script is run with a dot on the command line -- . batchstart.sh
How do I run it from java? My java code is below. It throws the following exception -
java.io.IOException: .: not found
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:102)
at java.lang.ProcessImpl.start(ProcessImpl.java:65)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)
at java.lang.Runtime.exec(Runtime.java:591)
at java.lang.Runtime.exec(Runtime.java:429)
at SetDisplay.main(SetDisplay.java:12)
import java.io.*;
public class SetDisplay {
public static void main(String[] args) {
File wd = new File("/myhomedir/");
System.out.println("Working Directory: " +wd);
Process proc = null;
try {
proc = Runtime.getRuntime().exec(". batchstart.sh", null, wd);
} catch (Exception e) {
e.printStackTrace();
}
}
}
How do I make the shell script run ?
I tried the following code as well, but that too doesn't work.
File wd = new File("/bin");
System.out.println(wd);
Process proc = null;
try {
proc = Runtime.getRuntime().exec("/bin/bash", null, wd);
}
catch (IOException e) {
e.printStackTrace();
}
if (proc != null) {
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
out.println("cd /home/");
out.println(". batchstart.sh");
out.println("exit");
try {
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
proc.waitFor();
in.close();
out.close();
proc.destroy();
}
catch (Exception e) {
e.printStackTrace();
}
}
When run from the command line, using a dot at the start of a script indicates that the script should be run in the current environment, instead of spawning a new subshell and using a new copy of the current environment. This allows you to export a new value of an environment variable to be used by commands run later from the same interactive shell.
Obviously, this technique only works if you are running your batchstart.sh script from an actual shell. Java does not know how this mechanism works and so the dot means nothing to it. A script cannot modify the environment of the Java process it was called from.
If your goal is to change the value of the DISPLAY environment variable for other commands run by your Java process, consider using the ProcessBuilder class to specify a new environment for the child process. Java does not contain a built-in way to modify variables in its own environment.
The source command (".") is a shell built-in. You have to explicitly run /bin/ksh, passing your script name as the argument (followed by any script arguments).
You have a larger problem if you need to source the script. That usually means that environment changes happen in the context of the current shell, not a subshell.
This won't work with Java since Java's not a shell. You'll need to figure out how to change the environment with Java.
Of course, if I'm wrong and there's more to that script that just setting DISPLAY, it may work as suggested.
The method you're going to have to use depends on what you're trying to achieve(as in "Are you running other programs using exec() that rely on DISPLAY being set?" or "Does your Java program need DISPLAY to be set?").
If, as you state in your comment, it's only your Java program that needs DISPLAY set, just set it outside before your program runs. Create a cmd (or bash) file which sets the DISPLAY variable then calls the JRE to run your program.
#/bin/ksh
export DISPLAY-:0.0
/usr/bin/jre/java your_program blah.blah.blah
I would also modify your main() to check that it's set to something and exit gracefully if not:
if (System.getenv ("DISPLAY") == null)
// doesn't exist, exit gracefully.
The period "." is a shell built-in, and executes the script "in-place", analogous to #include in C/C++. Using "." outside of a shell-script has no meaning.
If you want to run the script from Java, you have to execute the script interpreter (/bin/ksh):
Runtime.getRuntime().exec("/bin/ksh batchstart.sh", ...)
but note that this is not semantically equivalent, since you're executing batchstart.sh as a sub-process instead of sourcing it.

Problem with starting OpenOffice service (soffice) from Java (command working in commandline, but not from Java)

I want to exceute a simple command which works from the shell but doesn't work from Java.
This is the command I want to execute, which works fine:
soffice -headless "-accept=socket,host=localhost,port=8100;urp;"
This is the code I am excecuting from Java trying to run this command:
String[] commands = new String[] {"soffice","-headless","\"-accept=socket,host=localhost,port=8100;urp;\""};
Process process = Runtime.getRuntime().exec(commands)
int code = process.waitFor();
if(code == 0)
System.out.println("Commands executed successfully");
When I run this program I get "Commands executed successfully".
However the process is not running when the program finishes.
Is it possible that the JVM kills the program after it has run?
Why doesn't this work?
I'm not sure if I'm not mistaken, but as far as I see you're generating the commands but never passing them to the "execute" method... you're executing "".
Try using Runtime.getRuntime().exec(commands) =)
I would like to say how I solved this.
I created a sh script that basically run the command of soffice for me.
Then from Java I just run the script, and it works fine, like this:
public void startSOfficeService() throws InterruptedException, IOException {
//First we need to check if the soffice process is running
String commands = "pgrep soffice";
Process process = Runtime.getRuntime().exec(commands);
//Need to wait for this command to execute
int code = process.waitFor();
//If we get anything back from readLine, then we know the process is running
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
if (in.readLine() == null) {
//Nothing back, then we should execute the process
process = Runtime.getRuntime().exec("/etc/init.d/soffice.sh");
code = process.waitFor();
log.debug("soffice script started");
} else {
log.debug("soffice script is already running");
}
in.close();
}
I also kill the soffice process by calling this method:
public void killSOfficeProcess() throws IOException {
if (System.getProperty("os.name").matches(("(?i).*Linux.*"))) {
Runtime.getRuntime().exec("pkill soffice");
}
}
Note that this only works in Linux.
I believe you aren't handling quoting correctly. The original sh command line includes double quotes to prevent the shell interpreting the semicolons. The shell strips them off before the soffice process sees them.
In your Java code the shell will never see the arguments, so the extra double quotes (escaped with backslashes) are not needed - and they are probably confusing soffice.
Here's the code with the extra quotes stripped out (and a semicolon thrown in)
String[] commands = new String[] {"soffice","-headless","-accept=socket,host=localhost,port=8100;urp;"};
Process process = Runtime.getRuntime().exec(commands);
int code = process.waitFor();
if(code == 0)
System.out.println("Commands executed successfully");
(Disclaimer: I don't know Java, and I haven't tested this!)
"/Applications/OpenOffice.org\ 2.4.app/Contents/MacOS/soffice.bin -headless -nofirststartwizard -accept='socket,host=localhost,port=8100;urp;StartOffice.Service'"
or simply escaping the quotes will work as well. We feed a command like this to an ant script that ultimately ends up in an exec call like you have above. I would also recommend restarting the process every 500 or so conversions because OOO does not properly free memory (depending on what version you are running).

Categories

Resources