[英]Execution Problem
我已經調試了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();
}
}
}
編輯:關於您的程序:
while (i <= questions.size())
響起警鍾。 當變量i
等於questions.size()
您已經到達列表的末尾。 questions.get(i)
會引發異常,因為您正嘗試在列表之外閱讀。 該子句應閱讀while (i < questions.size()
。
原始消息:這是有關如何調試似乎“陷入循環”的程序的建議:
如果您在像Eclipse這樣的IDE中運行,則可以“暫停”當前正在調試的程序。 然后,通過查看調用堆棧,可以看到執行點當前所在的位置。 如果在系統方法中,則可以“設置返回”,直到執行點到達您的代碼為止。
如果循環中發生異常,則它永遠不會結束,因為您沒有break
。 也許您可以使用for循環。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.