简体   繁体   中英

Java simple networking program works in Eclipse, not in terminal

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)

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();
}

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.

Make sure that your scanner can handle different charsets/consoles correctly. It might be that one of the two setups uses eg 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).

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM