简体   繁体   English

从导出的.jar运行时,扫描程序会停止随机读取,而从Eclipse运行时,扫描程序不会停止读取

[英]Scanner stops reading randomly when ran from exported .jar but not when ran from Eclipse

so today I ran into some trouble while using the java Scanner. 所以今天我在使用java扫描器时遇到了一些麻烦。 I use the Scanner class many times in my project and I never ran into any problem. 我在项目中多次使用Scanner类,但从未遇到任何问题。

Basically, I always do something like this: 基本上,我总是这样做:

try (Scanner scanner = new Scanner(file)) {
    while(scanner.hasNextLine()) {
        String line = scanner.nextLine();
        ...
    }
} catch ...
} finally ...

and the Scanner works just fine because it's just some simple code. 扫描器可以正常工作,因为它只是一些简单的代码。 Today, however, I used the code above to read text files with about 17000 lines. 但是,今天,我使用上面的代码读取了大约17000行的文本文件。

At first the code worked just fine (when running it through Eclipse) as I expected but then, after exporting the project, the Scanner would stop reading after about 400 lines. 最初,代码按我期望的那样工作正常(通过Eclipse运行时),但是随后,在导出项目后,扫描程序将在大约400行之后停止读取。

I googled a bit and in the end I solved the problem thanks to these answers: 我搜索了一下,最后通过以下答案解决了这个问题:

All I had to do was change the constructor from 我要做的就是将构造函数从

Scanner scanner = new Scanner(file)

to

Scanner scanner = new Scanner(new FileInputStream(sql)))

It is some weird encoding problem, I get it. 这是一些奇怪的编码问题,我明白了。 But why when I ran the code from Eclipse it worked flawlessy and when I ran it from my exported jar the Scanner stopped after reading about 400 lines? 但是,为什么当我从Eclipse运行代码时,它就可以正常工作,而当我从导出的jar中运行代码时,扫描仪在读取大约400行后就停止了?

The code does the exact same thing in both cases because I set up Eclipse so that it would use the same working directory as the exported .jar archive (because it has got some data subdirectories): 在这两种情况下,代码都执行完全相同的操作,因为我设置了Eclipse,以便它将使用与导出的.jar存档相同的工作目录(因为它具有一些数据子目录):

  1. Takes the same .gz archive 使用相同的.gz存档
  2. Extract a file from the .gz 从.gz中提取文件
  3. Read the file like I showed above 像我上面显示的那样读取文件

Not sure if it helps but Eclipse is set up to save source files in UTF-8 format. 不确定是否有帮助,但是Eclipse已设置为以UTF-8格式保存源文件。

Thanks in advance 提前致谢

But why when I ran the code from Eclipse it worked flawlessy and when I ran it from my exported jar the Scanner stopped after reading about 400 lines? 但是,为什么当我从Eclipse运行代码时,它就可以正常工作,而当我从导出的jar中运行代码时,扫描仪在读取大约400行后就停止了?

Scanner has two different constructors that accept a File as argument. Scanner有两个不同的构造函数,它们接受File作为参数。 From the docs : 文档

Scanner(File source) 扫描仪(文件源)

Bytes from the file are converted into characters using the underlying platform's default charset . 使用基础平台的默认charset将文件中的字节转换为字符

and

Scanner(File source, String charsetName) 扫描仪(文件源,字符串charsetName)

Bytes from the file are converted into characters using the specified charset . 使用指定的字符集将文件中的字节转换为字符

So if you do not specify the charsetName , it will use the environment's default charset. 因此,如果不指定charsetName ,它将使用环境的默认字符集。

The environment encoding when you run your project outside Eclipse is probably other than UTF-8. 在Eclipse外部运行项目时,环境编码可能不是UTF-8。 To check that this is the case you can write a simple program like this: 要检查这种情况,可以编写一个简单的程序,如下所示:

class CheckDefaultCharset {
    public static void main(String... args) {
        System.out.println(Charset.defaultCharset());
    }
}

And run it on both environments. 并在两种环境下运行。

For example, when running the above code from Eclipse I get: 例如,从Eclipse运行上述代码时,我得到:

UTF-8

And when running in the PowerShell (Windows 7), I get: 在PowerShell(Windows 7)中运行时,我得到:

windows-1252

To avoid this type of problem it would be better to always specify the encoding of the files you intend to use when using Scanner . 为了避免此类问题,最好始终指定要使用Scanner时要使用的文件的编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从BufferedReader读取时发生NullPointerException,仅当以jar运行时 - NullPointerException when reading from BufferedReader, only when ran as a jar Java Jar文件从批处理文件运行时不发送电子邮件? - Java Jar file not sending email when ran from a batch file? 在JAVA中从JAR运行时如何获取定位文件的文件路径? - How to get filepath of the located file when ran from JAR in JAVA? 从已编译的jar运行时javafx.fxml.LoadException - javafx.fxml.LoadException when ran from a compiled jar 从Eclipse运行导出的JAR时出现NoClassDefFoundError - NoClassDefFoundError when running exported JAR from Eclipse 作为 a.jar 文件的程序比在 eclipse 中运行时慢 5 倍 - Program 5x slower as a .jar file than when ran in eclipse InputMismatchException 在我从 cmd 运行 jar 时发生,如果从idea 运行则不会发生 - InputMismatchException happens when I ran jar from cmd and doesn't happen if run from idea 在导出的 JAR 中加载图像,但从 eclipse 运行时不加载 - Images loading in exported JAR, but not when running from eclipse 为什么我的 Java 代码更改在从命令行运行但在 Eclipse 中运行时没有反映出来? - Why are my Java code changes not being reflected when ran from command line but working in Eclipse? 与从Netbeans运行时相比,Java程序作为dist生成要慢 - Java program is slower as a dist build than when ran from Netbeans
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM