【问题标题】:Java Apache Commons WordUtils.wrap() throws SecurityException during runtimeJava Apache Commons WordUtils.wrap() 在运行时抛出 SecurityException
【发布时间】:2012-09-26 22:29:23
【问题描述】:

好吧,所以在实现org.apache.commons.lang3.text.WordUtils 类之后,我希望能够使用WordUtils.wrap(String str, int width) 函数。但我遇到了减速带。

我能够毫无问题地编译这个程序(我应该提到的是一个小程序)。我只需要设置一个 CLASSPATH 环境变量来引用 apache jar 文件,然后通过 appletviewer 让程序运行。但是,当我到达使用 WordUtils.wrap() 函数的代码部分时,一切都变糟了,我在命令提示符下收到大约 20 行运行时错误。

错误:

Caught a SecurityException reading the system property 'awt.toolkit'; the System
Utils property value will default to null.
Caught a SecurityException reading the system property 'file.encoding'; the Syst
emUtils property value will default to null.
Caught a SecurityException reading the system property 'java.awt.fonts'; the Sys
temUtils property value will default to null.
Caught a SecurityException reading the system property 'java.awt.graphicsenv'; t
he SystemUtils property value will default to null.
Caught a SecurityException reading the system property 'java.awt.headless'; the
SystemUtils property value will default to null.
Caught a SecurityException reading the system property 'java.awt.printerjob'; th
e SystemUtils property value will default to null.
Caught a SecurityException reading the system property 'java.class.path'; the Sy
stemUtils property value will default to null.
Caught a SecurityException reading the system property 'java.compiler'; the Syst
emUtils property value will default to null.
Caught a SecurityException reading the system property 'java.endorsed.dirs'; the
 SystemUtils property value will default to null.
Caught a SecurityException reading the system property 'java.ext.dirs'; the Syst
emUtils property value will default to null.
Caught a SecurityException reading the system property 'java.home'; the SystemUt
ils property value will default to null.
Caught a SecurityException reading the system property 'java.io.tmpdir'; the Sys
temUtils property value will default to null.
Caught a SecurityException reading the system property 'java.library.path'; the
SystemUtils property value will default to null.
Caught a SecurityException reading the system property 'java.runtime.name'; the
SystemUtils property value will default to null.
Caught a SecurityException reading the system property 'java.runtime.version'; t
he SystemUtils property value will default to null.
Caught a SecurityException reading the system property 'java.util.prefs.Preferen
cesFactory'; the SystemUtils property value will default to null.
Caught a SecurityException reading the system property 'java.vm.info'; the Syste
mUtils property value will default to null.
Caught a SecurityException reading the system property 'user.country'; the Syste
mUtils property value will default to null.
Caught a SecurityException reading the system property 'user.region'; the System
Utils property value will default to null.
Caught a SecurityException reading the system property 'user.dir'; the SystemUti
ls property value will default to null.
Caught a SecurityException reading the system property 'user.home'; the SystemUt
ils property value will default to null.
Caught a SecurityException reading the system property 'user.language'; the Syst
emUtils property value will default to null.
Caught a SecurityException reading the system property 'user.name'; the SystemUt
ils property value will default to null.
Caught a SecurityException reading the system property 'user.timezone'; the Syst
emUtils property value will default to null.

这是导致所有这些麻烦的代码行:

String strWrap = WordUtils.wrap("A really really really really really long sentence.", 50);

这里发生了什么?

【问题讨论】:

  • 向我们展示更多代码如何?您的代码是否直接使用 SystemUtils? SystemUtils 正在尝试读取由于浏览器中的安全限制而可能不允许小程序读取的系统属性。
  • 没有在我的代码(这是一个 200 行的文件)中使用 SystemUtils,只使用 WordUtils...?

标签: java apache runtime-error word-wrap securityexception


【解决方案1】:

如何将行分隔符作为参数传递以绕过 SystemUtils 访问:

String strWrap = WordUtils.wrap("A really really really really really long sentence.", 50, "\n", false);

【讨论】:

  • 太棒了,解决了我的安全问题!但是,不,我有一个不同的问题,编程不是很有趣吗? :D ...好的,所以我使用了您的示例,而不是换行符,而是什么都没有,实际上什么都没有。而不是:“一个非常非常非常长的句子”我得到的是:“一个非常非常非常长的句子”?
  • 显示消息的控件可能需要 CRLF。尝试将“\n”替换为“\r\n”。这是不能使用系统定义的行分隔符的缺点。你必须猜。
  • 好吧,所以我做了一些挖掘(可能应该在所有这些之前)......原来我的命令:“Graphics.drawString()”不能插入新行或跳到下一行,它只能接受一个字符串并将整个事物作为像素图像输出在 ONE LINE 上。无论如何感谢您的所有帮助:)
【解决方案2】:

您的代码使用WordUtils 调用SystemUtils 来查找系统使用的行分隔符:

newLineStr = SystemUtils.LINE_SEPARATOR;

SystemUtils 正在尝试读取由于浏览器中的安全限制而可能不允许小程序读取的系统属性。

SystemUtils 的documentation 说:

如果由于安全限制而无法读取系统属性,则 此类中的相应字段将设置为null 和一条消息 将写入System.err

【讨论】:

  • 好吧,那么我该怎么做才能给我的小程序这个权限呢?
  • 您可能不需要,因为 SystemUtils 会捕获错误并继续执行。对WordUtils.wrap() 的调用是否成功?
  • 不幸的是,通过“捕获错误” SystemUtils 还停止了 WordUtils.wrap() 函数的执行,让我回到原点,找到一种包装我的文本的方法。那么,我还可以添加哪些其他代码行?或者,如果您知道更好的方法,我还能如何包装文本?
  • 除非您在 SystemUtils 尝试获取 line.separator 属性时也看到错误消息(我在上面的列表中没有看到这样的错误),否则 SystemUtils 不应阻止 WordUtils 工作。您是如何检查wrap() 不工作的 - 是否还有其他错误您没有向我们展示?
  • 我粘贴了我收到的整个回复。我知道 wrap() 不起作用,因为如果我将其注释掉并告诉我的小程序只绘制一些随机字符串,那么它就可以正常工作了。
猜你喜欢
  • 2011-11-04
  • 2016-11-12
  • 2023-01-29
  • 2015-10-09
  • 1970-01-01
  • 1970-01-01
  • 2018-01-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多