SAP GUI Java 7.80 LiveConnect脚本文件选择器权限错误求助
SAP GUI for Java 7.80 LiveConnect脚本文件选择器权限错误解决
问题重现
运行LiveConnect JavaScript脚本调用文件选择器时,抛出权限异常:
Exception: java.security.AccessControlException: access denied ("java.util.PropertyPermission" "com.sap.platin.base.util.GuiKeyboardFocusManager.focusLock" "read")
用户脚本代码:
var utils = application.utils; utils.showMessageBox("Info", "Starts OK", utils.MESSAGE_TYPE_INFORMATION, utils.MESSAGE_OPTION_OK); filePath=openFileChooser(); function openFileChooser () { var filePath; try { chooser = new Packages.javax.swing.JFileChooser(); retVal = chooser.showOpenDialog (null); if (retVal == Packages.javax.swing.JFileChooser.APPROVE_OPTION) filePath = chooser.getSelectedFile().getAbsolutePath(); }catch (e) { application.utils.showMessageBox ("Error in openFileChooser", "Exception: " + e, 0, 0); } return filePath; }
问题原因
该错误是Java安全管理器限制了LiveConnect脚本读取SAP内部系统属性com.sap.platin.base.util.GuiKeyboardFocusManager.focusLock,直接实例化Swing组件并传入null作为父容器时,会触发SAP GUI内部的焦点权限检查,进而触发安全拦截。
解决方案
方案1:调整脚本代码,使用SAP GUI容器作为父窗口
将showOpenDialog的参数从null改为SAP GUI的主窗口组件,避免触发焦点管理器的权限检查:
function openFileChooser () { var filePath; try { chooser = new Packages.javax.swing.JFileChooser(); // 使用SAP GUI主窗口作为文件选择器的父容器 var mainWindow = application.getMainWindow(); retVal = chooser.showOpenDialog (mainWindow.getAWTComponent()); if (retVal == Packages.javax.swing.JFileChooser.APPROVE_OPTION) filePath = chooser.getSelectedFile().getAbsolutePath(); }catch (e) { application.utils.showMessageBox ("Error in openFileChooser", "Exception: " + e, 0, 0); } return filePath; }
方案2:修改SAP GUI for Java安全策略文件
如果代码调整无效,需要给LiveConnect脚本添加对应的属性读取权限:
- 找到SAP GUI for Java的安全策略文件,Linux环境下常见路径:
- 系统级:
<SAP_GUI_INSTALL_DIR>/lib/security/java.policy - 用户级:
~/.sapgui/security/java.policy
- 系统级:
- 在策略文件中添加以下权限配置:
grant codeBase "file:${sapgui.home}/lib/liveconnect.jar" { permission java.util.PropertyPermission "com.sap.platin.base.util.GuiKeyboardFocusManager.focusLock", "read"; }; - 重启SAP GUI for Java使配置生效。
注意事项
- 优先尝试方案1,修改安全策略会降低系统安全性,仅在代码调整无效时使用。
- Linux环境下修改系统级策略文件需要sudo权限,确保文件修改后权限为
root:root、权限值644。
内容的提问来源于stack exchange,提问作者IvanSTV
相关产品推荐
相关产品推荐

