【问题标题】:Execution Problem执行问题
【发布时间】:2011-05-09 07:09:31
【问题描述】:

我已经调试了我的 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();
        }
    }
}

【问题讨论】:

  • 没有任何代码,我们帮不了你。我们没有任何精神力量。
  • Debug-Execution Adventure 的可能重复项
  • 鉴于您有一个空的 catch(Exception e) {} 块,您如何确定没有抛出异常?
  • 在那个特定位置,如果抛出异常,那么(根据代码)什么都不应该发生(因为在 catch 块中没有给出任何内容)。那就是循环再次开始的时候,因为如果没有抛出异常,那么执行就会结束。

标签: java eclipse debugging execution


【解决方案1】:

编辑:关于您的程序:

while (i &lt;= questions.size()) 敲响了警钟。当变量 i 等于 questions.size() 时,您已经到达列表的末尾。 questions.get(i) 将抛出异常,因为您正在尝试读取列表之外的内容。该子句应为while (i &lt; questions.size()

原始消息:这是关于如何调试似乎“陷入循环”的程序的建议:

如果您在 Eclipse 之类的 IDE 中运行,您可以“暂停”您当前正在调试的程序。然后通过查看调用堆栈,您可以看到当前执行点的位置。如果它在系统方法中,您可以“设置返回”,直到执行点到达您的代码。

【讨论】:

  • 我明白......它在调试代码时工作得很好 - 它根本没有突出任何问题。但是在运行时,虽然条件已经满足,但 while 循环第三次没有工作。
  • 就我而言,这有关系吗?因为,在我拥有的 3 次迭代中,前 2 次迭代的 i=0(根据我的逻辑,这就是它应该表现的方式)。在第 3 次迭代期间,我的 i 值将增加。
【解决方案2】:

如果您的循环中发生异常,它将永远不会结束,因为您没有break。也许您可以使用 for 循环。

【讨论】:

  • 我处理得当也不会结束吗?因为调试器说我处理得当。
  • 我不知道“调试器说我处理得当”是什么意思。但正如 Jim 已经告诉你的,如果发生错误,变量“i”不会增加。所以你将永远重试这个问题。
  • 是的,没有编译错误,据我说也没有逻辑错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多