简体   繁体   English

行为差异:Intellij vs Eclipse

[英]Behaviour Difference: Intellij vs Eclipse

I am trying to import and run existing and working Java code into Intellij but I ran into few problems and this causes me to write this question here: 我试图将现有的和有效的Java代码导入并运行到Intellij中,但是遇到了几个问题,这使我在这里写下了这个问题:

My Storm topology (java) code is reading data from Websphere MQ and the recieved message which is a byte-stream and parsed by a "Message Parser" project; 我的Storm拓扑(java)代码正在从Websphere MQ和接收到的消息中读取数据,该消息是字节流,并由“消息解析器”项目进行了解析; it parses these bytestream "String" and generates meaningful message based on some rules. 它解析这些字节流“字符串”,并根据一些规则生成有意义的消息。

When I run it in Eclipse it works without any issue, but in Intellij it shows me problem related to message parser. 当我在Eclipse中运行它时,它没有任何问题,但是在Intellij中,它向我显示了与消息解析器有关的问题。 I senses it as encoding issue and tried to: 我将其视为编码问题,并尝试:

  • Change the code file encoding to UTF-8 将代码文件编码更改为UTF-8
  • Change line separator to become identical (Unix) 更改行分隔符使其相同(Unix)

But this did not lead me to resolution. 但这并没有导致我解决。

Because you all have good expertise in Java IDEs, hence I expect you must have faced the IDE's compatibility issues for Java code many of the times. 由于你们所有人都具有Java IDE方面的专业知识,因此,我希望您必须多次面对IDE的Java代码兼容性问题。 Kindly let me know if there is any way to resolve this problem. 请让我知道是否有任何方法可以解决此问题。

The code where the problem is occuring is given below: 发生问题的代码如下:

public void execute(Tuple input) {

    String strMessage = null;
    Message posMsg = null;
                Object jmsMsg = input.getValueByField(FieldEnum.FIELD_MESSAGE
                .getFieldName());
        posMsg = ((JMSMessage) jmsMsg);
        strMessage = convertStreamToString(posMsg);

        System.out.println("Message recieved from MQ : "+ strMessage);

        @SuppressWarnings("rawtypes")
        Map parsedSegments = MessageParser.instance().parseMessage(
                strMessage);

        @SuppressWarnings("serial")
        Type nposMessageType = new TypeToken<Map<String, Map<String, String>>>() {
        }.getType();
        String segmentsJson = gson.toJson(parsedSegments, nposMessageType);

        System.out.println(" testing the messages "+segmentsJson);
MessageDetail MessageDetail = new MessageDetail(
                Constant.TOPOLOGY_NAME, segmentsJson);
        this.outputCollector.emit(Constant.STREAM_MSG_PARSER_SUCCESS,
                new Values(MessageDetail));
        this.outputCollector.ack(input);
}


        /**
 * Convert stream to string
 * 
 * @param jmsMsg
 * @return
 * @throws Exception
 */
private static String convertStreamToString(final Message jmsMsg) throws Exception {
    String stringMessage = "";
    BytesMessage bMsg = (BytesMessage) jmsMsg;
    byte[] buffer = new byte[40620];
    int byteRead;
    ByteArrayOutputStream bout = new java.io.ByteArrayOutputStream();
    while ((byteRead = bMsg.readBytes(buffer)) != -1) {
        bout.write(buffer, 0, byteRead);
    }
    bout.flush();
    stringMessage = new String(bout.toByteArray());
    bout.close();
    return stringMessage;
}

Your code is to blame. 您的代码应该受到指责。 You're using 您正在使用

new String(bout.toByteArray());

to convert bytes to characters. 将字节转换为字符。 That uses the platform default encoding. 使用平台默认编码。 It's different when running from Eclipse than it is when running with IntelliJ, because IntelliJ, if I'm not mistaken, passes a JVM option forcing the default encoding to UTF-8. 从Eclipse运行时与通过IntelliJ运行时有所不同,因为如果我没有记错的话,IntelliJ会传递一个JVM选项,将默认编码强制为UTF-8。

But that's not an IntelliJ problem. 但这不是IntelliJ问题。 Your code would fail the same way if you were running your program out of any IDE, on a platform whose default encoding is UTF-8 (Linuxes and Macs, for example). 如果在默认编码为UTF-8的平台上(例如Linuxes和Macs)在任何IDE之外运行程序,则代码将以相同的方式失败。

Every time you transform bytes into chars and vice-versa, make sure to specify an encoding explicitely. 每次将字节转换为char时,反之亦然,请确保明确指定编码。 If MQ is generating the message, read its documentation to know which encoding it uses, and make sure to use the same one. 如果MQ正在生成消息,请阅读其文档以了解其使用的编码,并确保使用相同的编码。 For example, if it's ISO-8859-1, use 例如,如果是ISO-8859-1,请使用

new String(bytes, StandardCharsets.ISO_8859_1)

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

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