Execute R Script from Web Application with Java - java

I have created a Web Application with Struts, and I would like to execute and R script from a Java class.
My script is in the directory: WebContent/script
The name is: rScript.R
I have checked that a way to execute is using this:
Runtime.getRuntime().exec("WebContent/script/rScript.R")
But this doesn't work. Does someone know how I have to put the sentence?
Cheers.

Try
Process process =Runtime.getRuntime().exec("RScript C:/.../WebContent/script/rScript.R")
process.waitFor();

Related

Batch file run command after previous command has finished executing

I am trying to clear a batch file, which does some Weblogic Admin server data source update after which I need to restart the admin server..I am trying to automate the same through batch file...So I have;
call wlst UpdateDataSource.py
stopWebLogic.cmd weblogicUser weblogicPwd localhost:7001
startWebLogic.cmd
Now, how do I ensure that startWebLogic.cmd is executed only after the previous line has finished executing (i.e. after stopWebLogic.cmd finishes)
I'm assuming Windows, since you have .cmd files!?
You can use & between the scripts to run them in sequence. If you use && the ensuing scripts will only run if the previous ones completed successfully.
You can read more here.
Cheers,

Writing CGI script in Java

I trying to figure out how to write CGI scripts in Java.
I followed this examples -> http://www.javaworld.com/jw-01-1997/jw-01-cgiscripts.html?page=1
It provide cgi_lib.java, hello.html and hello.java
Everything seems fine, but in the html part.
The action is pointed to cgi_lib/hello.cgi
There's no cgi provided. So I tried with cgi_lib/hello.java, and it print the entire source code in the hello.java.
Then i tried to edit the hello.java extensions into hello.cgi, and tried again.
The browser returns me error 500.
What is the problem?
Is it that, there's some specific method to compile the hello.java into hello.cgi?
The script in hello.cgi is different from hello.java?
Please help.
Thank you.
UPDATE
I added hello.cgi
#!/bin/sh
java -Dcgi.content_type=$CONTENT_TYPE -Dcgi.content_length=$CONTENT_LENGTH - Dcgi.request_method=$REQUEST_METHOD -Dcgi.query_string=$QUERY_STRING -Dcgi.server_name=$SERVER_NAME -Dcgi.server_port=$SERVER_PORT -Dcgi.script_name=$SCRIPT_NAME -Dcgi.path_info=$PATH_INFO hello
So is the $CONTENT_TYPE, $CONTENT_LENGTH,... remain the same? Or should I enter something?
Just to make things clearer.
I put the the cgi_lib, hello.java and hello.cgi in the C:\xampp\cgi-lib
And the hello.html in C:\xampp\htdocs\test
When I tried to connect it returns me this
Server error!
The server encountered an internal error and was unable to complete your request.
Error message:
couldn't create child process: 720002: hello.cgi
If you think this is a server error, please contact the webmaster.
Error 500
localhost
Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7
I had check the httpd.conf in c:\xampp\apache\conf and configure according to this
LoadModule cgi_module modules/mod_cgi.so
ScriptAlias /cgi-bin/ "C:/Program Files/Apache Group/Apache/cgi-bin/"
Seems to be already enable.
So whats the problem right now?
The contents of hello.cgi is shown at the top of page 2 of the article.
#!/bin/sh
java -Dcgi.content_type=$CONTENT_TYPE -Dcgi.content_length=$CONTENT_LENGTH -Dcgi.request_method=$REQUEST_METHOD -Dcgi.query_string=$QUERY_STRING -Dcgi.server_name=$SERVER_NAME -Dcgi.server_port=$SERVER_PORT -Dcgi.script_name=$SCRIPT_NAME -Dcgi.path_info=$PATH_INFO hello
The article says that you will execute the java jar from within a cgi script. You need to make sure that this script is set up to execute your jar file by making the correct calls to the java executable with your hello.jar file as the parameter. Also make sure your web server is configured correctly to allow the execution of cgi scripts.
You also have to chmod of your cgi and java files so others can execute them.

JRuby script with Rubeus and Swing exiting once packaged into jar using warble

I am trying to package a simple JRuby script into a jar file.
The script uses Rubeus::Swing and runs correctly when executed with the JRuby interpreter.
require 'rubygems'
require 'rubeus'
class Example01
extend Rubeus::Swing
def show
JFrame.new("Rubeus Swing Example 01") do |frame|
frame.visible = true
end
end
end
Example01.new.show
Once I package the script into a JAR with warble, when I execute:
java -jar jtest.jar
... the JFrame window shows up and instantly closes.
There is no indication of errors of any kind.
Does anyone know why this happens?
Warbler calls System.exit() after your main script exits. This causes the Swing EventThread to exit, closing your app.
https://github.com/jruby/warbler/blob/master/ext/JarMain.java#L131
I worked around this problem by joining with the event thread at the bottom of my start script like so:
event_thread = nil
SwingUtilities.invokeAndWait { event_thread = java.lang.Thread.currentThread }
event_thread.join
Hacky, but it works.
Just set the appropriate flag:
System.setProperty("warbler.skip_system_exit","true");

running build.xml from java program

i am trying to run ant script from java program.what is the procedure to execute the program
How to run build.xml from java program?
here is how iam trying to implement
Process proc = rt.exec("ant -buildfile D:ant\\trail");
regards,
techie
Check here Execute Ant From Your Application
and look at this example:
Project project = new Project();
project.init();
DefaultLogger logger = new DefaultLogger();
logger.setMessageOutputLevel(Project.MSG_INFO);
logger.setErrorPrintStream(System.err);
logger.setOutputPrintStream(System.out);
project.addBuildListener(logger);
File buildFile = new File("buildhtml.xml");
ProjectHelper.configureProject(project, buildFile);
project.setProperty("ant.file", buildFile.getAbsolutePath());
project.setProperty("item", "ant");
project.setProperty("inputdir", "src/items/ant");
project.setProperty("outputdir", "build/items/ant");
project.setProperty("graphics.prefix", "../../");
try {
project.executeTarget("checkifuptodate");
} catch(Exception e) {System.err.println(e.getMessage());}
// rest of program goes here
It is a better solution than calling Runtime.exec
Rather than trying to start a windows executable separately, it'd be a more robust and flexible solution to use the Ant API. Docs are included with ant itself, they are not online...
Try Runtime.getRuntime().exec("cmd /c start ant.bat");
taken from How do I run a batch file from my Java Application?

IntelliJ Plugin - Run Console Command

I am new to plugin development for IntelliJ and would like to know, how I can execute a command in the command line from within my plugin.
I would like to call, for instance, the command "gulp" in the current projects root directory.
I already tried using
Runtime.getRuntime().exec(commands);
with commands like "cd C:\Users\User\MyProject" and "gulp", but it does not seem to work that way and I wonder, if the plugin API provides an easier method.
I know its a bit late (1 year later), but recently I was working on an IntelliJ plugin and I had the same issue and this is what I used and it works pretty well.
First, we need to create a list of commands that we need to execute:
ArrayList<String> cmds = new ArrayList<>();
cmds.add("./gradlew");
Then
GeneralCommandLine generalCommandLine = new GeneralCommandLine(cmds);
generalCommandLine.setCharset(Charset.forName("UTF-8"));
generalCommandLine.setWorkDirectory(project.getBasePath());
ProcessHandler processHandler = new OSProcessHandler(generalCommandLine);
processHandler.startNotify();
hence the generalCommandLine.setWorkDirectory is set to the project directory which could be equivalent to the terminal command cd path/to/dir/
The Runtime class provides exec(String[], String[], File) method where the last argument is working directory of the subprocess being launched.
The plugin API provides OSProcessHandler class (as well as other classes like ProcessAdapter) which can help to manage the subprocess, handle its output etc.
ProcessOutput result1 = ExecUtil.execAndGetOutput(generalCommandLine);
result1.getStdOut result1.getStdErr works
and
ScriptRunnerUtil.getProcessOutput(generalCommandLine, ScriptRunnerUtil.STDOUT_OUTPUT_KEY_FILTER, timeout);
both work pretty well
and they are built into intellij
import com.intellij.execution.process.ScriptRunnerUtil;
import com.intellij.execution.util.ExecUtil;

Categories