Java Eclipse-从命令提示符运行时的输出与通过IDE不同

时间:2018-10-07 14:08:19

标签: java eclipse

这是在超级用户https://superuser.com/questions/1364594/java-eclipse-output-when-running-from-command-prompt-is-not-the-same-as-throug上提出的,但有人告诉我在这里询问。

我对Java和Eclipse IDE还是很陌生,在Windows的命令提示符下运行Java代码时遇到问题。如果我通过Eclipse IDE运行代码,则输出为

func1(param) {...}
func2(param1, param2) {...}

mapFunction(list, callback) {
 //....
 const something = 'blabla';
 // if callback has second param run callback(something, secondParam)
 return callback(something) 
}

mapFunction(list, func1);
mapFunction(list, func2); // send second param

但是,如果我通过命令提示符Message number 0 Message number 1 Message number 2 Timeout. Client is closing. 运行代码,则命令提示符下的消息之间会出现很多空行。

java test.Main

我尝试添加简单的检查,以在字符串为NULL或为空但不起作用时不输出任何输出。下面是我的代码。我花了很多时间,但是不知道是什么原因造成的。

  

Main.java

//Hundreds of empty lines 
Message number 0
//Hundreds of empty lines
Message number 1
//Hundreds of empty lines
Message number 2
//Hundreds of empty lines
Timeout. Client is closing.
  

UdpUnicastClient.java

package test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

// create 5 processes

public class Main {

    public static void main(String[] args) {
        int port = 50001;
        UdpUnicastServer server = new UdpUnicastServer(port);
        UdpUnicastClient client = new UdpUnicastClient(port);

        ExecutorService executorService = Executors.newFixedThreadPool(2);
        executorService.submit(client);
        executorService.submit(server);
    }
}
  

UdpUnicastServer.java

package test;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * Created by dan.geabunea on 6/3/2016.
 */
public class UdpUnicastClient implements Runnable {
    private final int port;

    public UdpUnicastClient(int port) {
        this.port = port;
    }

    @Override
    public void run() {
        /**
         * Bind the client socket to the port on which you expect to
         * read incoming messages
         */
        try (DatagramSocket clientSocket = new DatagramSocket(port)) {
            /**
             * Create a byte array buffer to store incoming data. If the message length
             * exceeds the length of your buffer, then the message will be truncated. To avoid this,
             * you can simply instantiate the buffer with the maximum UDP packet size, which
             * is 65506
             */

            byte[] buffer = new byte[65507];

            // Set a timeout of 3000 ms for the client.
            clientSocket.setSoTimeout(3000);
            while (true) {
                DatagramPacket datagramPacket = new DatagramPacket(buffer, 0, buffer.length);

                /**
                 * The receive method will wait for 3000 ms for data.
                 * After that, the client will throw a timeout exception.
                 */
                clientSocket.receive(datagramPacket);

                String receivedMessage = new String(datagramPacket.getData());
                if(receivedMessage != null && !receivedMessage.isEmpty()) {
                    System.out.println(receivedMessage);
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("Timeout. Client is closing.");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我怀疑是字符集(编码)问题。

尝试将字符集名称传递给服务器应用程序中的String.getBytes()和客户端应用程序中的String构造函数,例如

   byte[] bytes = message.getBytes("UTF-8");
    DatagramPacket datagramPacket = new DatagramPacket(
                            bytes,
                            bytes.length,
                            InetAddress.getLocalHost(),
                            clientPort
                    );

String receivedMessage = new String(datagramPacket.getData(), "UTF-8");
  • 编辑-还应确保对出站数据报使用正确的长度,要认识到以char为单位的字符串长度不一定是字节数组的长度(以字节为单位)。更新了示例代码。