简体   繁体   English

在控制台中抑制rjava错误输出

[英]suppress rjava error output in console

How can I suppress the rJava output to the console in the following example? 在以下示例中,如何抑制rJava输出到控制台?

library(rJava)
TC <- J("edu.cens.spatial.RTileController")
         dummy <- capture.output(suppressWarnings(suppressMessages(
res <- TC$getInstance(type="osm-bw")$getTileValues(4389,2691,13)
         )))

Despite capture.output , I still get the following in the console: 尽管有capture.output ,但在控制台中我仍然得到以下内容:

java.lang.NullPointerException
    at edu.cens.spatial.RTileController.getTileValues(RTileController.java:109)
    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:498)
    at RJavaTools.invokeMethod(RJavaTools.java:386)

Edit : In pure R Console (without Rstudio), I get no messages (but I have to call library("OpenStreetMap") first). 编辑 :在纯R控制台(没有Rstudio)中,我没有收到任何消息(但必须先调用library("OpenStreetMap") )。 So this might be an Rstudio issue after all ... The question is now: how can I suppres Java output to the R console in Rstudio? 因此,这毕竟可能是Rstudio的问题。现在的问题是: 如何在Rstudio中禁止Java输出到R控制台? Is it possible to do this when calling osmtile as outlined below? 如下所述调用osmtile时可以这样做吗?

PS1: It works for osm instead of osm-bw . PS1:它适用于osm而不是osm-bw
PS2: I came across this via PS2:我通过

tile <- OpenStreetMap::osmtile(x=4389,y=2691,zoom=13,type="osm-bw")

In my case, something like this works - NullPointer message is suppressed: 在我的情况下,类似这样的工作-NullPointer消息被抑制:

> s <- .jcall(obj, returnSig="V", method="nullcall")
Error in .jcall(obj, returnSig = "V", method = "nullcall") :
  java.lang.NullPointerException: Exception
> suppressMessages(s <- .jcall(obj, returnSig="V", method="nullcall"))

To reproduce this code do following: 要重现此代码,请执行以下操作:

  1. Create file (from within R) 创建文件(从R内部)

     dir.create("utils") dir.create("target") cat('package utils; public class RUsingStringArray { public void nullcall() throws NullPointerException { throw new NullPointerException("Exception"); } public static void main(String [] arg) { RUsingStringArray obj = new RUsingStringArray(); obj.nullcall(); } }', file="utils/RUsingStringArray.java") 
  2. Compile java code (in cmd / terminal, last line won't work on windows) 编译Java代码(在cmd /终端中,最后一行在Windows上不起作用)

     javac -d target utils/*.java java -cp target utils/RUsingStringArray Exception in thread "main" java.lang.NullPointerException: Exception at utils.RUsingStringArray.nullcall(RUsingStringArray.java:19) at utils.RUsingStringArray.main(RUsingStringArray.java:24) export CLASSPATH=`pwd`/target 
  3. Inside R 内部R

     library(rJava) .jinit("C:/path_to_folder/target") # leave empty if CLASSPATH was set obj <- .jnew("utils.RUsingStringArray") s <- .jcall(obj, returnSig="V", method="nullcall") suppressMessages(s <- .jcall(obj, returnSig="V", method="nullcall")) Error in .jcall(obj, returnSig = "V", method = "nullcall") : java.lang.NullPointerException: Exception 

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

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