将jdb作为java应用程序中的进程运行时,输出不完整

时间:2015-07-27 03:24:30

标签: java jdb

我正在尝试从java程序调试java类。我创建了一个简单的类来测试。这是我的班级hello.java,它位于C:\Users\madhawax\Desktop\beaufify\debugging

文件夹中

我的问题是我无法检索部分

VM Started: Set deferred breakpoint Hello.main
...

当我从java代码运行jdb时,但是当我从命令行手动运行jdb时,我可以看到它。

为什么我只获得部分实际输出?我该如何解决这个问题?

这是我的Hello.java课程:

public class Hello {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            System.out.println("loop number "+i);
        }
    }
} 

我使用3个命令来运行jdb

jdb
stop in Hello.main
run Hello

使用cmd手动调试时的控制台输出。

C:\Users\madhawax\Desktop\beaufify\debugging>jdb
Initializing jdb ...
> stop in Hello.main
Deferring breakpoint Hello.main.
It will be set after the class is loaded.
> run Hello
run  Hello
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
>
VM Started: Set deferred breakpoint Hello.main

Breakpoint hit: "thread=main", Hello.main(), line=3 bci=0
3            for (int i = 0; i < 10; i++) {

main[1]

使用java代码运行jdb时的输出。

run:
Initializing jdb ...
> Deferring breakpoint Hello.main.
It will be set after the class is loaded.
> run  Hello
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
> Input stream closed.
BUILD STOPPED (total time: 4 seconds)

我使用此代码运行jdb。

try {
    ProcessBuilder builder = new ProcessBuilder("C:\\Program Files\\Java\\jdk1.8.0_31\\bin\\jdb.exe");
    builder.directory(new File("C:\\Users\\madhawax\\Desktop\\beaufify\\debugging\\"));
    Process process = builder.start();
    OutputStream stdin = process.getOutputStream();
    InputStream stdout = process.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
    try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin))) {
        writer.write("stop in Hello.main\n");
        writer.flush();
        writer.write("run Hello");
        writer.flush();
    }
    String inputLine;
    Scanner scanner = new Scanner(stdout);
    while (scanner.hasNextLine()) {
        System.out.println(scanner.nextLine());
    }
 } catch (IOException ex) {
    ex.printStackTrace();
}

1 个答案:

答案 0 :(得分:2)

我建议您在没有try-with-resources的情况下运行代码:

Scanner scanner = new Scanner(stdout);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
writer.write("stop in Hello.main\n");
writer.flush();
writer.write("run Hello\n");
writer.flush();

while (scanner.hasNextLine()) {
    System.out.println(scanner.nextLine());
}

在您的代码中try-with-resources将在执行后关闭BufferedWriter:

try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin)))     {
    writer.write("stop in Hello.main\n");
    writer.flush();
    writer.write("run Hello");
    writer.flush();
} //writer is closed here

因此,它将关闭底层进程输出流,这显然会导致jdb进程关闭。

您可能希望将try-with-resource更改为try-catch-finally包装整个方法。

更新:此外,在运行next命令之前,读取一个命令的输出是个好主意。用你的方法:

writer.write("stop in Hello.main\n");
writer.flush();
writer.write("run Hello\n");
writer.flush();
writer.write("list\n");
...

命令调用之间没有暂停。 jdb当时可能无法处理list命令(因为它启动了VM)。作为实验,您可以引入时间差:

writer.flush();
Thread.sleep(1000);
writer.write("list\n");

优越的方法是读取两者之间的输出。

writer.flush();
readOutput(stdout);
writer.write("list\n");

您可以使用扫描仪读取输出。但是,正如@vandale在评论中指出的那样,Scanner会阻止令牌和换行符。您可能希望使用非阻塞读取来读取可用输出。这样的事情可能有用:

private void readOutput(InputStream outputStream) throws IOException{
    byte[] buffer = new byte[100000];
    int bytesRead;
    while (outputStream.available() > 0) {
        bytesRead = outputStream.read(buffer);
        if (bytesRead > 0) {
            System.out.print(new String(buffer, 0, bytesRead));
        }
    }
}

此代码还将显示不以换行符结束的输出(输入提示,main[1]等)。