Aaryan
Aaryan

Reputation: 25

Runtime.getRuntime().exec gets no results when used from java netbeans

I am working on a Java project, and stuck at a step as it is showing no results.

The java code is:

public class callPgs {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    try{    
        Process p = Runtime.getRuntime().exec("C:/R/R-3.2.0/bin/Rscript.exe C:/R/R-3.2.0/bin/arrayqualityMetrics.R");

        int processComplete = p.waitFor();

                   if (processComplete == 0) {

                        JOptionPane.showMessageDialog(null,"Process is completed");
                        System.out.println("successfull");
                   } else {
                       JOptionPane.showMessageDialog(null,"You Have selected Wrong Input File");
                        System.out.println("Could not complete");
                   }
                }
                catch (Exception e1)
                {
                               e1.printStackTrace();
                }
    }

}

as I am executing this by using command prompt using Rscript.exe

C:\R\...\bin\Rscript "arrayqualityMetrics.R"

I am getting my results correctly but when I am doing it by Java netbeans using the above code, it is showing else option as "Could not complete" in the netbean's console.

As other RScripts are running good and without any error but this one is not giving the results, as the Rscript is for arrayqualityMetrics as:-

library(arrayQualityMetrics)

library(limma)

library(tcltk)

options( warn = -1 ) #commands for removing warnings as they are comming when running the Rscript from the R console.

X <-tk_choose.files(caption = "Choose Files")

maData<-read.maimages(X, source="agilent", other.columns = "gProcessedSignal", green.only=TRUE)

eSet<-new("ExpressionSet", exprs = maData$other$gProcessedSignal, annotation =maData$genes[,7])

arrayQualityMetrics(eSet, outdir="QC_C", force = TRUE, do.logtransform = TRUE)

So I want to ask weather there is any error in the Rscript or there is any thing I should add in the java code to run it properly.... Any help is appreciable......

Upvotes: 2

Views: 1095

Answers (3)

Scary Wombat
Scary Wombat

Reputation: 44854

Unless you connect to the processes inputstream then you will not be able to capture it.

See

http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html#getInputStream()

as in

 InputStream os = p.getInputStream ();
 while ((b = os.read ()) != -1) {
    System.out.print (b);
 }

Upvotes: 2

user3145373 ツ
user3145373 ツ

Reputation: 8156

You can do :

try{    
        Process p = Runtime.getRuntime().exec("C:\\R\\R-3.2.0\\bin\\Rscript.exe C:\\dir\my_script.R");

        int processComplete = p.waitFor();

           if (processComplete == 0) {
                System.out.println("successfull");
           } else {
                System.out.println("Could not complete");
           }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

try to add full path to your file that need to be executed. I have tried with this simple example and that works fine for me.

Process process = Runtime.getRuntime().exec("notepad C:\\abc.txt");

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201507

You need to waitFor the Process to finish. Per that Javadoc, that causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. Also, you'll probably want to add a catch for the possible InterruptedException. So, something like

try {
    Process p = Runtime.getRuntime().exec(
            "C:/R/R-3.2.0/bin/Rscript.exe my_script.R");
    p.waitFor();
} catch (IOException e) {
    e.printStackTrace();
} catch (InterruptedException e) {
    e.printStackTrace();
}

Upvotes: 1

Related Questions