我正在使用 Eclipse 开发一个 Java 程序,并认为如果没有参数,我会在我的程序中添加一个选项来解析标准输入。(否则它会解析一个文件)
如果我执行"somecommand | java -jar myjar.jar"
并去调试,我会遇到问题......然后意识到我不知道如何在 Eclipse 中启动这样的进程。如果我在命令提示符下运行它,我无法附加到正在运行的进程,因为该进程会立即启动。
关于如何调试的任何建议?
编辑:看,问题是,我最初编写程序是为了获取文件名参数。然后我认为它也可以使用标准输入,所以我从我的程序中抽象了 InputStream(正如 Queue 先生所建议的那样)。它在文件(java -jar myjar.jar myfile
)上运行良好,但在我运行时无法运行type myfile | java -jar myjar.jar
。我怀疑这两种情况有一些不同(eof 检测不同?)但我真的很想调试。
// overall program structure follows:
public static void doit(InputStream is)
{
...
}
public static void main(String[] args)
{
if (args.length > 0)
{
// this leaves out the try-catch-finally block,
// but you get the idea.
FileInputStream fis = new FileInputStream(args[0]);
doit(fis);
fis.close();
}
else
{
doit(System.in);
}
}