且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

阅读直到EOF在cygwin上的java在windows-10

更新时间:2023-12-03 23:10:28

在非Cygwin Windows程序中,Ctrl-Z输入触发文件结束条件。



在Cygwin程序中,使用Cygwin DLL,Ctrl-D输入触发器和文件结束条件(这可以重新配置为 stty 命令,但你还没有这样做。)





您正在运行的程序是 java 命令,它是一个非Cygwin Windows可执行文件。 ( java 命令正好运行你的Java程序。)由于 java 是一个非Cygwin Windows程序将Ctrl-Z作为文件结束字符。



据我所知,你描述的所有行为都是一致的。



如果你想让Java程序像Cygwin程序,你必须使用一个建立使用Cygwin的Java系统。我不知道这是否可行,但它超出了这个问题的范围,所以我会留给你和一个谷歌搜索。


I am using a java utility (antlr) on cygwin, the java is the windows version and I run javac/java from the cygwin bash.

The following java code used to work on cygwin on win8.1, to read a series of numbers from stdin till EOF (adapted from reading input till EOF in java):

import java.util.Scanner;
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, World");
        Scanner in = new Scanner(System.in);
        int num;
        while (in.hasNextInt()) {
            num = in.nextInt();
            System.out.printf("%d\n",num);
        }
    }
}

Recently I got a new win10 laptop, installed cygwin on it, and it does not work anymore. When I press Ctrl-D, the program continues to wait for input, it is not taken as an EOF.

After some debugging (see comments in what is EOF in cygwin in windows 10), "stty -a" reports "eof = ^D; susp = ^Z", so the Ctrl-D is expected to be seen as EOF. The following equivalent code in c++ works as expected, possibly because this code is compiled with cygwin itself and hence treats ^D as EOF.

#include <stdio.h>
int main() {
    int i=0, status;
    for(;;) {
      printf("Enter a value %d:\n", i);
      status = scanf("%d", &i);
      if(status!=1) break;
    }
}

So something seems to be broken in the combination of cygwin-win10. Any hints, on what can be broken and how to fix it?

NOTE: I am using java jdk-7u72

When using cygwin, I use the java installation of windows itself. Here are the usages:

In cygwin: javac Hello.java; java Hello

Then give the inputs, followed by ^D in a new line (ie immediately after entering Return.

In windows: I also tried running the same program in windows, just to make sure whether it is a windows or cygwin problem. So here also I run: javac Hello.java; java Hello

Then give the inputs, followed by ^D (or ^Z) in a new line (ie immediately after entering Return). And the program terminates correctly, when I press ^D or ^Z.

In a non-Cygwin Windows program, Ctrl-Z on input triggers an end-of-file condition.

In a Cygwin program, one that uses the Cygwin DLL, Ctrl-D on input triggers and end-of-file condition. (This can be reconfigured with the stty command, but you haven't done that.)

It doesn't matter how the program is executed. It only matters how it was built.

The program you're running is the java command, which is a non-Cygwin Windows executable. (The java command happens to be running your Java program.) Since java is a non-Cygwin Windows program, it treats Ctrl-Z as the end-of-file character.

As far as I can tell, all the behavior you describe is consistent with that.

If you want Java programs to act like Cygwin programs, you'd have to use a Java system built to use Cygwin. I'm not sure whether that's feasible, but it's beyond the scope of this question, so I'll leave that to you and a Google search.