Runtime.getRuntime().exec 从 java netbeans 使用时没有结果
Runtime.getRuntime().exec gets no results when used from java netbeans
我正在处理一个 Java 项目,由于没有显示任何结果而卡在了某个步骤。
java代码是:
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();
}
}
}
因为我正在使用 Rscript.exe
使用命令提示符执行此操作
C:\R\...\bin\Rscript "arrayqualityMetrics.R"
我得到的结果是正确的
但是当我使用上面的代码通过 Java netbeans 执行此操作时,它在 netbean 的控制台中将 else 选项显示为 "Could not complete"。
因为其他 RScripts 运行ning 很好并且没有任何错误但是这个没有给出结果,因为 Rscripts 用于 arrayqualityMetrics as:-
库(arrayQualityMetrics)
图书馆(limma)
图书馆(tcltk)
options(warn = -1) #commands for remove warnings as when they are coming when 运行ning the Rscript from the R console.
X <-tk_choose.files(标题="Choose Files")
maData<-read.maimages(X, source="agilent", other.columns = "gProcessedSignal", green.only=TRUE)
eSet<-new("ExpressionSet", exprs = maData$other$gProcessedSignal, 注释=maData$genes[7])
arrayQualityMetrics(eSet, outdir="QC_C", force = TRUE, do.logtransform = TRUE)
所以我想问一下 Rscript 中是否有任何错误,或者我应该在 java 代码中添加任何内容以正确 运行 它....
任何帮助都是可观的......
您需要 waitFor
Process
才能完成。根据该 Javadoc,如果需要, 会导致当前线程等待,直到此 Process
对象表示的进程终止。 此外,您可能想要添加一个抓住可能的 InterruptedException
。所以,像
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();
}
除非您连接到进程输入流,否则您将无法捕获它。
见
http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html#getInputStream()
如
InputStream os = p.getInputStream ();
while ((b = os.read ()) != -1) {
System.out.print (b);
}
你可以这样做:
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();
}
}
尝试为需要执行的文件添加完整路径。
我试过这个简单的例子,对我来说效果很好。
Process process = Runtime.getRuntime().exec("notepad C:\abc.txt");
我正在处理一个 Java 项目,由于没有显示任何结果而卡在了某个步骤。
java代码是:
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();
}
}
}
因为我正在使用 Rscript.exe
使用命令提示符执行此操作C:\R\...\bin\Rscript "arrayqualityMetrics.R"
我得到的结果是正确的 但是当我使用上面的代码通过 Java netbeans 执行此操作时,它在 netbean 的控制台中将 else 选项显示为 "Could not complete"。
因为其他 RScripts 运行ning 很好并且没有任何错误但是这个没有给出结果,因为 Rscripts 用于 arrayqualityMetrics as:-
库(arrayQualityMetrics)
图书馆(limma)
图书馆(tcltk)
options(warn = -1) #commands for remove warnings as when they are coming when 运行ning the Rscript from the R console.
X <-tk_choose.files(标题="Choose Files")
maData<-read.maimages(X, source="agilent", other.columns = "gProcessedSignal", green.only=TRUE)
eSet<-new("ExpressionSet", exprs = maData$other$gProcessedSignal, 注释=maData$genes[7])
arrayQualityMetrics(eSet, outdir="QC_C", force = TRUE, do.logtransform = TRUE)
所以我想问一下 Rscript 中是否有任何错误,或者我应该在 java 代码中添加任何内容以正确 运行 它.... 任何帮助都是可观的......
您需要 waitFor
Process
才能完成。根据该 Javadoc,如果需要, 会导致当前线程等待,直到此 Process
对象表示的进程终止。 此外,您可能想要添加一个抓住可能的 InterruptedException
。所以,像
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();
}
除非您连接到进程输入流,否则您将无法捕获它。
见
http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html#getInputStream()
如
InputStream os = p.getInputStream ();
while ((b = os.read ()) != -1) {
System.out.print (b);
}
你可以这样做:
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();
}
}
尝试为需要执行的文件添加完整路径。 我试过这个简单的例子,对我来说效果很好。
Process process = Runtime.getRuntime().exec("notepad C:\abc.txt");