Java 执行问题
我已经调试了我的java代码。它似乎没有任何语法或逻辑错误。但是当我执行代码时,它不会终止,也不会抛出任何错误。有谁能帮我想出另一个办法来解决这个问题吗 这是我的shell脚本-Java 执行问题,java,eclipse,debugging,execution,Java,Eclipse,Debugging,Execution,我已经调试了我的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" ];
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();
}
}
}
包装;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入java.util.List;
输入expectj.expectj;
输入期望产卵;
公共类表示扩展了线程{
公共静态StringBuffer执行(字符串cmd,列出问题){
效用u=新效用();
StringBuffer sb=新的StringBuffer();
ExpectJ exp=新的ExpectJ();
InputStreamReader isr=新的InputStreamReader(System.in);
BufferedReader br=新的BufferedReader(isr);
字符串应答=null;
cmd=“sh/root/Desktop/details.sh”;
试一试{
Spawn s=exp.Spawn(cmd);
问题q=零;
int i=0;
而(iEdit):关于您的程序:
while(i如果在你的循环中发生异常,它将永远不会结束,因为你没有中断
。也许你可以使用for循环来代替。没有任何代码,我们无法帮助你。我们没有任何精神力量。如果你有一个空的捕获(异常e){},则可能会重复
block,您如何确定没有抛出异常?在该特定位置,如果抛出异常,则(根据代码)不会发生任何事情(因为catch块中没有给出任何内容)。这是循环再次开始的时候,因为如果没有抛出异常,那么执行将结束。我理解..调试代码时工作正常-根本没有突出显示任何问题。但是在运行期间,尽管条件满足,while循环第三次不工作。在我的情况下,这有关系吗?Because,在我的3次迭代中,前2次迭代的i=0(根据我的逻辑,这就是它的行为)。在第3次迭代中,我的i值将增加。即使我正确地处理它,它不会结束吗?因为调试器说我正确地处理了它。我不知道是什么“调试器说我正在正确处理它”的意思是。但正如Jim已经告诉过你的,如果发生错误,变量“I”将不会增加。因此你将永远重试这个问题。是的,没有编译错误,根据我的说法,也没有逻辑错误。