运行时出现java错误。使用EclipseNeon的sh文件
我在youtube上观看了一段关于添加数字的视频
他创造了一个新的世界。在终端中使用vim的sh文件,代码如下:
read num1
read num2
sum=`expr $num1 + $num2`
echo $sum
我也做了同样的操作,并将文件保存在/home/dell中(“dell”是用户名)。使用执行时/添加数字。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&;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项目启动文件,这是我想要执行的第一步,但是我遇到了错误。 提前谢谢
编辑: 问题解决了,我把命令写错了。它应该是/home/dell/addNumbers。sh,而不是/home/dell/添加数字。嘘
共 (0) 个答案