阅读直到EOF在cygwin上的java在windows-10 [英] reading till EOF in java on cygwin on windows-10

查看:176
本文介绍了阅读直到EOF在cygwin上的java在windows-10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在cygwin上使用一个java工具(antlr),java是windows版本,我从cygwin bash运行javac / java。



用于在win8.1上运行cygwin的代码,从stdin直到EOF读取一系列数字(改编自在java中读取输入直到EOF < a>):

  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\\\
,num);
}
}
}



最近我获得了一个新的win10笔记本电脑,安装cygwin就可以了,它不工作了。当我按下Ctrl-D,程序继续等待输入,它不会被当作EOF。



经过一些调试(参见在Windows 10中的cygwin中是EOF ),stty -a报告eof = ^ D; susp = ^ Z,因此Ctrl-D预计将被视为EOF。下面的c ++中的等效代码与预期的一样,可能因为这个代码是用cygwin本身编译的,因此将^ D作为EOF。

  #include< stdio.h> 
int main(){
int i = 0,status;
for(;;){
printf(输入值%d:\\\
,i);
status = scanf(%d,& i);
if(status!= 1)break;
}
}

所以有些东西似乎在cygwin -win10。任何提示,什么可以打破和如何解决它?



注意:我使用java jdk-7u72



当使用cygwin时,我使用java安装的windows本身。以下是用法:



在cygwin中: javac Hello.java; java Hello



然后给出输入,接着在新行中输入^ D(即在输入Return之后立即输入)。



在窗口中:我也试过在Windows中运行相同的程序,只是为了确保它是一个Windows或cygwin问题所以这里也运行:javac Hello.java; p>

然后给出输入,然后在新行中输入^ D(或^ Z)(即在输入Return之后立即),并且程序正确终止,当我按^ D或^ Z。

解决方案

在非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.

这篇关于阅读直到EOF在cygwin上的java在windows-10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆