【问题标题】:How do I stop Java/Eclipse from bleeding stdout and stderr together?我如何阻止 Java/Eclipse 一起流出 stdout 和 stderr?
【发布时间】:2015-05-20 07:09:18
【问题描述】:

所以这是我正在处理的程序中得到的输出:

java.lang.IllegalArgumentException: argument type mismatch
CreationDate
getCreationDate
setCreationDate
LastModifiedDate
getLastModifiedDate
setLastModifiedDate
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at Transform.transform(Transform.java:86)
    at Experiment.main(Experiment.java:120)
java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at Transform.transform(Transform.java:86)
    at Experiment.main(Experiment.java:120)
DeletionDate
getDeletionDate
setDeletionDate

调用它的代码:

    for (Field f : databaseFields) {
        String upperCamelCase = Character.toUpperCase(f.getName().charAt(0)) + f.getName().substring(1);
        System.out.println(upperCamelCase);

        String getMethodName = "get" + upperCamelCase;
        System.out.println(getMethodName);

        Method getMethod = null;
        try {
            getMethod = databaseClass.getDeclaredMethod(getMethodName);
        } catch (NoSuchMethodException e) { 
        } catch (SecurityException e) { }

        if (getMethod == null) {
            getMethodName = "is" + upperCamelCase;
            try {
                getMethod = databaseClass.getDeclaredMethod(getMethodName);
            } catch (NoSuchMethodException e) {
                continue;
            } catch (SecurityException e) {
                continue;
            }
        }

        String setMethodName = "set" + upperCamelCase;
        System.out.println(setMethodName);
        Method setMethod = null;
        for (int i = 0; i < parameterTypes.length; i++) {
            try {
                setMethod = jsonClass.getDeclaredMethod(setMethodName, parameterTypes[i]);
            } catch (NoSuchMethodException e) { }
        }

        if (setMethod == null) {
            continue;
        }

        try {
            setMethod.invoke(jsonObject, getMethod.invoke(databaseObject));
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

这是用于反射变换的,显然我不知道它在哪个 get 或 set 方法上引发了异常。我该如何解决这个问题?

【问题讨论】:

  • 左键单击括号中的 Experiment 或 Transform 以跳转到导致异常结束的代码行。在这种情况下,Transform 的第 86 行,由 Experiment 的第 120 行调用。
  • @GilbertLeBlanc 我添加了代码。第 86 行是setMethod.invoke。上面的printlns 都是输出。

标签: java eclipse stdout stderr


【解决方案1】:

在 Eclipse 中,Console 中的 stderr 和 stdout 的颜色不同。您还可以通过右键单击控制台窗口并选择首选项来隐藏其中一个。

在命令行中,您可以使用重定向 &gt;2&gt; 将 stderr 或 stdout 发送到单独的文件。

【讨论】:

  • 好吧,问题是我不想/不需要将它们分开。我需要某种方式来判断哪一行标准输出对应于来自标准错误的异常。
【解决方案2】:

打电话

System.out.flush();

在每个println 调用后立即强制输出​​到控制台。

【讨论】:

  • 我很生气,以至于我没有在我的问题中提出“Java 中是否有与 cin.flush() 等效的内容?”因为我就是这么想的。我认为如果我提到输出问题的输入类,那听起来会很愚蠢。
  • 我必须在每个堆栈跟踪之后添加System.err.flush(),但它有效!
猜你喜欢
  • 2016-09-04
  • 1970-01-01
  • 1970-01-01
  • 2016-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-22
  • 1970-01-01
相关资源
最近更新 更多