如何将控制台输入传递给正在运行的Java程序而不是jdb?

时间:2009-12-05 22:09:36

标签: java debugging jdb

使用Java的jdb调试我的代码。我停留在我的程序需要命令行输入的位置,但是jdb将其作为jdb命令拦截。

如何告诉jdb将文本传递给正在运行的程序?

版本:

C:\Documents and Settings\*snip*>java -showversion
java version "1.6.0_17"
Java(TM) SE Runtime Environment (build 1.6.0_17-b04)
Java HotSpot(TM) Client VM (build 14.3-b01, mixed mode, sharing)

编译:

javac -g LZWDecompress.java

jdb.ini:

stop in LZWDecompress.decompress
run
monitor list

这就是:

Initializing jdb ...
*** Reading commands from C:\Documents and Settings\*snip*\jdb.ini
Deferring breakpoint LZWDecompress.decompress.
It will be set after the class is loaded.
> run LZWDecompress
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
> > >
VM Started: Set deferred breakpoint LZWDecompress.decompress
File to be decompressed: 

在上面的提示中,我输入“test”,并收到此回复:

...
VM Started: Set deferred breakpoint LZWDecompress.decompress
File to be decompressed: test
Unrecognized command: 'test'.  Try help...
>

这是函数main(...)的代码,由我们的教师编写,而不是我:

public static void main(String[] args) throws IOException {    
    short [] source; 
    int dlen; 
    int sz;
    byte [] decompressed;
    BufferedReader br;
    DataInputStream In;
    FileInputStream FI;
    FileOutputStream FO;
    InputStreamReader isr;
    String cfnm;
    String sfnm;

    source = new short[INPUT_FILE_IO_BUFFER_SIZE];
    decompressed = new byte[OUTPUT_FILE_IO_BUFFER_SIZE];

    isr = new InputStreamReader(System.in);
    br = new BufferedReader(isr);
    System.out.print("File to be decompressed: ");
    System.out.flush(); 
    sfnm = br.readLine();
    System.out.print("Name of the decompressed file: ");
    System.out.flush(); 
    cfnm = br.readLine();
    FI = new FileInputStream(sfnm);
    In = new DataInputStream(FI);
    FO = new FileOutputStream(cfnm);
    for (sz=0; true; ++sz) {
        try { source[sz] = In.readShort();
        } catch (EOFException e) { break;
        } catch (Exception e) { System.out.print(e.toString()); 
        }
    }
    dlen = decompress(source, sz, decompressed);
    FO.write(decompressed, 0, dlen);
    FO.close(); 
}  // end main()

1 个答案:

答案 0 :(得分:6)

嗯......这个post似乎表明我需要运行程序,然后将调试器附加到它。例如:

% java -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n LZWDecompress

% jdb -connect com.sun.jdi.SocketAttach:hostname=localhost,port=8000

在通过用户输入函数后,如何让程序停止,并不是很明显。

这是首选方法吗?

更新:一旦正在运行的程序提示用户输入,请附加并配置jdb(例如,断点)。配置jdb后,为正在运行的程序提供输入。然后jdb在第二个窗口中接管程序执行。 :d