我关注添加数字的YouTube视频:https://www.youtube.com/watch?v=cOYYzikLGoY
他在终端中使用 vim 创建一个.sh文件,这是代码:
read num1
read num2
sum=`expr $num1 + $num2`
echo $sum
我做了同样的事,并将文件保存在/ home / dell中(' dell'是用户名)。当它在终端中使用 ./addNumbers.sh 命令执行时,它会被完美地执行 - 请求两个数字,并输出结果。
现在,我想运行同样的东西,但是在Eclipse中。 java中的代码是:
package opening_prog;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.*;
import java.io.*;
public class opening_prog
{
public static void main(String args[]) throws IOException
{
String command[]= {"/home/dell./addNumbers.sh"};
ProcessBuilder pb = new ProcessBuilder(command); //process builder is ready with the environment
for (int i = 1; i<=5; i++){
for(int j = 1; j<=5; j++){
Process p = pb.start(); //process instance variable, throws declaration
//instantiation, and running
//starts a new process, which is the path to process builder, the shell code
OutputStream os = p.getOutputStream(); //code has already started but waiting for values
//pipe connected to output stream
PrintStream ps = new PrintStream(os); //connects the input to the shell with the java OutputStream
ps.println(Integer.toString(i));
ps.flush();//buffer so flush it again
ps.println(Integer.toString(j));
ps.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
//reader ready, now just to output
String cOutput;
while((cOutput=br.readLine()) != null){
System.out.println("Sum of "+i+" & "+j+" is "+cOutput);
}
}
}
}
}
我应该得到的输出是: 1&amp;的总和1是2 等 但相反,我遇到了错误,例如:
Exception in thread "main" java.io.IOException: Cannot run program "/home /dell./addNumbers.sh": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at opening_prog.opening_prog.main(opening_prog.java:51)
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:247)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
... 1 more
我可以知道这是什么问题。我想通过Eclipse中的GUI为我的ROS项目启动.launch文件,这是我想要执行的第一步,但是我遇到了错误。 提前谢谢。
编辑: 问题解决了,我写错了&#34;命令&#34; 。它应该是 /home/dell/addNumbers.sh ,而不是/home/dell./addNumbers.sh