首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >执行问题

执行问题
EN

Stack Overflow用户
提问于 2011-05-09 07:09:31
回答 2查看 158关注 0票数 1

我已经调试了我的java code.It,它似乎没有任何语法或逻辑错误。但是,当我执行代码时,它也没有终止,也没有抛出任何错误。有人能帮我解决这个问题吗?

这是我的外壳脚本-

代码语言:javascript
复制
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代码-

代码语言:javascript
复制
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();
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-05-09 07:15:12

编辑:关于您的程序:

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

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

如果您正在Eclipse这样的IDE中运行,您可以“挂起”当前正在调试的程序。然后,通过查看调用堆栈,您可以看到当前执行点的位置。如果是在系统方法中,则可以“安装返回”,直到执行点到达代码为止。

票数 2
EN

Stack Overflow用户

发布于 2011-05-09 07:17:35

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5933696

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档