执行问题
我已经调试了我的java代码。它似乎没有任何语法或逻辑错误。但是当我执行代码时,它不会终止,也不会抛出任何错误。有人可以帮我用另一种方法来处理这个问题吗?
这是我的 shell 脚本 -
echo "Name"
read name
if [ "$name" == "abcd" ]; then
echo "correct name"
else
echo "wrong name"
fi
echo "Password"
read password
if [ "$password" == "pwd" ]; then
echo "Correct password"
else
echo "Wrong password"
fi
echo "City"
read city
if [ "$city" == "bangalore" ]; then
echo "correct city"
else
echo "wrong city"
fi
这是我的 java 代码 -
package Pack;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import expectj.ExpectJ;
import expectj.Spawn;
public class Presentation extends Thread {
public static StringBuffer execute(String cmd, List<Question> questions) {
Utility u = new Utility();
StringBuffer sb = new StringBuffer();
ExpectJ exp = new ExpectJ();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String answer = null;
cmd = "sh /root/Desktop/details.sh";
try {
Spawn s = exp.spawn(cmd);
Question q = null;
int i = 0;
while (i <= questions.size()) {
System.out.println("iteration " + i);
q = questions.get(i);
try {
if (s.getCurrentStandardOutContents().contains(
q.getQuestion())) {
i++;
}
s.expect(q.getQuestion(), q.timeoutInSec);
if (q.isInteractive) {
System.out.println("Please provide your input: ");
answer = br.readLine();
} else {
if (q.isAnswerEncrypted) {
// TODO: decrypt the answer
} else {
answer = q.getAnswer();
}
}
s.send(answer + "\n");
i++;
try {
s.expectClose(3);
System.out.println("Script completed");
break;
} catch (Exception e) {
}
} catch (Exception e) {
System.out.println("Timeout!!!Please answer "
+ s.getCurrentStandardOutContents());
try {
answer = u.PromptUserForAnswerInCaseOfException();
s.send(answer + "\n");
} catch (IOException ioe) {
System.out.println("IO Exception..");
}
}
}
s.expectClose();
} catch (IOException ioe) {
System.out.println("No more communication due to the lack of data");
} catch (Exception e) {
}
return sb;
}
public static void main(String[] args) {
String cmd = "sh /root/Desktop/details.sh";
List<Question> questions = new ArrayList<Question>();
Question question1 = new Question();
question1.setQuestion("Name");
question1.setIsInteractive(false);
question1.setAnswer("abcd");
question1.setIsAnswerEncrypted(false);
Question question2 = new Question();
question2.setQuestion("Password");
question2.setIsInteractive(true);
question2.timeoutInSec = 5;
question2.setAnswer("pwd");
question2.setIsAnswerEncrypted(false);
Question question3 = new Question();
question3.setQuestion("City");
question3.setIsInteractive(false);
question3.timeoutInSec = 5;
question3.setAnswer("bangalore");
question3.setIsAnswerEncrypted(false);
questions.add(question2);
questions.add(question1);
questions.add(question3);
System.out.println(questions.toString());
try {
execute(cmd, questions);
} catch (Exception e) {
e.printStackTrace();
}
}
}
I have debugged my java code.It doesn't seem to have any syntactical or LOGICAL errors. But when i execute the code, it is not terminating and not throwing any errors either. Can anybody please help me out with another way to deal with this?
This is my shell script -
echo "Name"
read name
if [ "$name" == "abcd" ]; then
echo "correct name"
else
echo "wrong name"
fi
echo "Password"
read password
if [ "$password" == "pwd" ]; then
echo "Correct password"
else
echo "Wrong password"
fi
echo "City"
read city
if [ "$city" == "bangalore" ]; then
echo "correct city"
else
echo "wrong city"
fi
This is my java code -
package Pack;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import expectj.ExpectJ;
import expectj.Spawn;
public class Presentation extends Thread {
public static StringBuffer execute(String cmd, List<Question> questions) {
Utility u = new Utility();
StringBuffer sb = new StringBuffer();
ExpectJ exp = new ExpectJ();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String answer = null;
cmd = "sh /root/Desktop/details.sh";
try {
Spawn s = exp.spawn(cmd);
Question q = null;
int i = 0;
while (i <= questions.size()) {
System.out.println("iteration " + i);
q = questions.get(i);
try {
if (s.getCurrentStandardOutContents().contains(
q.getQuestion())) {
i++;
}
s.expect(q.getQuestion(), q.timeoutInSec);
if (q.isInteractive) {
System.out.println("Please provide your input: ");
answer = br.readLine();
} else {
if (q.isAnswerEncrypted) {
// TODO: decrypt the answer
} else {
answer = q.getAnswer();
}
}
s.send(answer + "\n");
i++;
try {
s.expectClose(3);
System.out.println("Script completed");
break;
} catch (Exception e) {
}
} catch (Exception e) {
System.out.println("Timeout!!!Please answer "
+ s.getCurrentStandardOutContents());
try {
answer = u.PromptUserForAnswerInCaseOfException();
s.send(answer + "\n");
} catch (IOException ioe) {
System.out.println("IO Exception..");
}
}
}
s.expectClose();
} catch (IOException ioe) {
System.out.println("No more communication due to the lack of data");
} catch (Exception e) {
}
return sb;
}
public static void main(String[] args) {
String cmd = "sh /root/Desktop/details.sh";
List<Question> questions = new ArrayList<Question>();
Question question1 = new Question();
question1.setQuestion("Name");
question1.setIsInteractive(false);
question1.setAnswer("abcd");
question1.setIsAnswerEncrypted(false);
Question question2 = new Question();
question2.setQuestion("Password");
question2.setIsInteractive(true);
question2.timeoutInSec = 5;
question2.setAnswer("pwd");
question2.setIsAnswerEncrypted(false);
Question question3 = new Question();
question3.setQuestion("City");
question3.setIsInteractive(false);
question3.timeoutInSec = 5;
question3.setAnswer("bangalore");
question3.setIsAnswerEncrypted(false);
questions.add(question2);
questions.add(question1);
questions.add(question3);
System.out.println(questions.toString());
try {
execute(cmd, questions);
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:关于你的程序:
while (i <= questions.size())
敲响了警钟。当变量i
等于questions.size()
时,您已经到达列表的末尾。questions.get(i)
将抛出异常,因为您试图读取列表之外的内容。该子句应为while (i < questions.size()
)。原始消息:这是关于如何调试似乎“卡在循环中”的程序的建议:
如果您正在运行在像 Eclipse 这样的 IDE 中,您可以“暂停”当前正在调试的程序,然后通过查看调用堆栈,您可以看到当前执行点在哪里,如果它在系统方法中,您可以“设置返回”直到执行点。到达您的代码。
Edit: Regarding your program:
while (i <= questions.size())
rings alarm bells. When variablei
is equal toquestions.size()
you have already hit the end of the list.questions.get(i)
will throw an exception because you are trying to read outside of the list. The clause should readwhile (i < questions.size()
.Original message: This is advice on how to debug a program that appears to be 'stuck in a loop':
If you are running in an IDE like Eclipse you can 'suspend' the program you are currently debugging. Then by looking at the call stack you can see where the execution point is currently. If it is in a system method you can 'setup return' until the execution point reaches your code.
如果循环中发生异常,它将永远不会结束,因为您没有
break
。也许你可以使用 for 循环。If an exception occur within your loop it will never end, as you have no
break
. Maybe you could use a for loop instead.