开发者

Execution Problem

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 = ne开发者_开发技巧w 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();
        }
    }
}


Edit: Regarding your program:

while (i <= questions.size()) rings alarm bells. When variable i is equal to questions.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 read while (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.


If an exception occur within your loop it will never end, as you have no break. Maybe you could use a for loop instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜