Java简单网络程序在Eclipse中运行,而不是在终端中运行

发布于 2024-10-11 04:00:43 字数 636 浏览 2 评论 0原文

我用 Java 编写了一个简单的网络程序,它从服务器端的文本文件中读取文本并将其发送到客户端。客户端程序将文本写入客户端计算机上的文本文件。

我正在一台计算机 (localhost) 上测试该程序,它可以在 Eclipse 中运行,但是当我尝试从终端运行它时,我在服务器端收到运行时错误。从服务器的文本文件中读取文本的扫描仪似乎有问题,但我确信。

这是错误:

线程“main”中的异常 java.lang.NullPointerException 在 java.util.regex.Matcher.toMatchResult(libgcj.so.10) 在 java.util.Scanner.myCoreNext(libgcj.so.10) 在 java.util.Scanner.myPrepareForNext(libgcj.so.10) 在 java.util.Scanner.myNextLine(libgcj.so.10) 在 java.util.Scanner.hasNextLine(libgcj.so.10) 在 pkg.TextTransmitServer.sendText(TextTransmitServer.java:50) 在 pkg.TextTransmitServer.main(TextTransmitServer.java:26)

I wrote a simple networking program in Java that reads text from a text file on the server side and sends it to the client. The client program writes the text to a text file on the client computer.

I'm testing the program on one computer (localhost), and it works in Eclipse but when I try to run it from the terminal, I get a runtime error on the server side. It seems to be a problem with the Scanner that reads the text from the server's text file, but I'm note sure.

Here is the error:

Exception in thread "main" java.lang.NullPointerException
at java.util.regex.Matcher.toMatchResult(libgcj.so.10)
at java.util.Scanner.myCoreNext(libgcj.so.10)
at java.util.Scanner.myPrepareForNext(libgcj.so.10)
at java.util.Scanner.myNextLine(libgcj.so.10)
at java.util.Scanner.hasNextLine(libgcj.so.10)
at pkg.TextTransmitServer.sendText(TextTransmitServer.java:50)
at pkg.TextTransmitServer.main(TextTransmitServer.java:26)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

明月夜 2024-10-18 04:00:43

仔细检查您在这两种情况下是否使用相同的输入文件。打开文件时是否使用完全限定路径?如果这些文件不同,就可以解释为什么正则表达式适用于其中一个而不适用于另一个。

请在问题中添加两件事。 1) 文件打开方式的代码片段,以及 2) 正则表达式用法的代码片段。有趣的观察:为什么 java.util.Scanner.hasNextLine(libgcj.so.10) 使用正则表达式?一定要传一份进去吗?

不确定为什么要使用“hasNextLine()”。将分隔符设置为行分隔符后尝试使用 hasNext() 。请参阅 此页面了解更多示例。

   private static void readFile(String fileName) {
     try {
       Scanner scanner = new Scanner(new File(fileName));
       scanner.useDelimiter
         (System.getProperty("line.separator")); 
       while (scanner.hasNext()) {
         System.out.println(scanner.next());
       scanner.close();
     } catch (FileNotFoundException e) {
       e.printStackTrace();
     }
   } 

hasNextLine() 使用的分隔符为 null,或者扫描的输入行为 null。当抛出 NPE 时,从文件中打印出有问题的输入行。如果没有您的代码,这只是在黑暗中拍摄,但类似这样:

[in or close pkg.TextTransmitServer.sendText(TextTransmitServer.java:50) ]

try {
   String currentLine = null;
   String previousLine = null;
   while (scanner.hasNext()) {
      previousLine = currentLine;
      currentLine= parseLine(scanner.next());
   }   
}
catch (NullPointerException npe) {
   System.out.println("previous line: " + previousLine);
   System.out.println("current line: " + currentLine);
   npe.printStackTrace();
}

Double check that you are using the same input file in both cases. Are you using fully qualified paths when you open the file? If the files are different, it would explain why a regular expression works with one and not the other.

Please add two things to the question. 1) code snippet of how the file is opened, and 2) code snippet of the regex usage. Interesting observation: why is java.util.Scanner.hasNextLine(libgcj.so.10) using a regex? Did you have to pass one in?

Not sure why you are using 'hasNextLine()'. Try with hasNext() after setting the delimiter to be the line separator. See this page for more examples.

   private static void readFile(String fileName) {
     try {
       Scanner scanner = new Scanner(new File(fileName));
       scanner.useDelimiter
         (System.getProperty("line.separator")); 
       while (scanner.hasNext()) {
         System.out.println(scanner.next());
       scanner.close();
     } catch (FileNotFoundException e) {
       e.printStackTrace();
     }
   } 

Either the delimiter being used by hasNextLine() is null or the inputline being scanned is null. Print out the offending input line from the file when the the NPE is thrown. Without your code this is just a shot in the dark but something like this:

[in or near pkg.TextTransmitServer.sendText(TextTransmitServer.java:50) ]

try {
   String currentLine = null;
   String previousLine = null;
   while (scanner.hasNext()) {
      previousLine = currentLine;
      currentLine= parseLine(scanner.next());
   }   
}
catch (NullPointerException npe) {
   System.out.println("previous line: " + previousLine);
   System.out.println("current line: " + currentLine);
   npe.printStackTrace();
}
无声静候 2024-10-18 04:00:43

作为找到问题根源的指针,您可以通过将以下参数添加到 java 命令来从命令行调试 java 应用程序 -

-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000

然后使用 8000 端口将 eclipse 连接到远程调试器。这将帮助您轻松查明问题的根源。

As a pointer in getting to the root of the issue you can debug your java application from command line by adding the following parameters to your java command-

-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000

And then connect eclipse to the remote debugger using 8000 port. This will help you easily get to the bottom of the issue.

叫嚣ゝ 2024-10-18 04:00:43

确保您的扫描仪可以正确处理不同的字符集/控制台。两种设置之一可能使用例如 UTF-8,因此可能会看到单个字符的多字节(或者如果您的协议对字符串的长度进行编码,则相反)。

Make sure that your scanner can handle different charsets/consoles correctly. It might be that one of the two setups uses e.g. UTF-8 and may thus see multi-bytes for a single character (or rather the other way round if your protocol encodes the length of the string).

旧城烟雨 2024-10-18 04:00:43

好的,首先,正如 @Kelly 所说,检查您在这两种情况下是否正在读取相同的文件。

现在,我感觉您已将整个项目从 Sun JVM(桌面上的 Eclipese)移至另一个 JVM(从您的异常情况可以清楚地看出您正在 Linux、默认 JVM 上运行)。这可能是由于该 JVM 中的错误造成

的使用 Sun 的 JVM 而不是 GCJ(Linux 上的默认 JVM)

Ok, first, as @Kelly states check if you are reading same file in both cases.

Now, I get a feeling that you moved your whole project from Sun JVM ( Eclipese on your desktop ) to another JVM ( from your exceptions it is clear that you are running on Linux , default JVM ) . This might be due to a bug in that JVM

Consider using Sun's JVM instead of GCJ ( default JVM on linux)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文