阿十六 2024-07-09 14:56 采纳率: 80%
浏览 36

JAVA运行时报错Unable to make protected void java.awt.EventQueue.dispatchEvent

我将matlab程序打包成了一个jar包,想要在JAVA中调用,一开始可以正常运行
我想在Jframe中运行它,因此我将运行它的代码放在了构造方法内,一直报错

java.lang.reflect.InaccessibleObjectException: Unable to make protected void java.awt.EventQueue.dispatchEvent(java.awt.AWTEvent) accessible: module java.desktop does not "opens java.awt" to unnamed module @37574691
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
    at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199)
    at java.base/java.lang.reflect.Method.setAccessible(Method.java:193)
    at com.mathworks.toolbox.javabuilder.internal.MCRThreadUtilities$1.run(MCRThreadUtilities.java:76)
    at com.mathworks.toolbox.javabuilder.internal.MCRThreadUtilities$1.run(MCRThreadUtilities.java:71)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:569)
    at com.mathworks.toolbox.javabuilder.internal.MCRThreadUtilities.<clinit>(MCRThreadUtilities.java:70)
    at com.mathworks.toolbox.javabuilder.internal.MWMCR$4.invoke(MWMCR.java:835)
    at jdk.proxy1/jdk.proxy1.$Proxy0.mclInitializeComponentInstance(Unknown Source)
    at com.mathworks.toolbox.javabuilder.internal.MWMCR.newInstance(MWMCR.java:646)
    at forJava2.ForJava2MCRFactory.newInstance(ForJava2MCRFactory.java:44)
    at forJava2.ForJava2MCRFactory.newInstance(ForJava2MCRFactory.java:55)
    at forJava2.forJava.<init>(forJava.java:63)
    at utils.Matlab.Tri(Matlab.java:15)
    at demo.tttt$1.mouseClicked(tttt.java:45)
    at java.desktop/java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:278)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6629)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389)
    at java.desktop/java.awt.Component.processEvent(Component.java:6391)
    at java.desktop/java.awt.Container.processEvent(Container.java:2266)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4584)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:746)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:744)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Exception in thread "AWT-EventQueue-0": java.lang.reflect.UndeclaredThrowableException
    at jdk.proxy1/jdk.proxy1.$Proxy0.mclInitializeComponentInstance(Unknown Source)
    at com.mathworks.toolbox.javabuilder.internal.MWMCR.newInstance(MWMCR.java:646)
    at forJava2.ForJava2MCRFactory.newInstance(ForJava2MCRFactory.java:44)
    at forJava2.ForJava2MCRFactory.newInstance(ForJava2MCRFactory.java:55)
    at forJava2.forJava.<init>(forJava.java:63)
    at utils.Matlab.Tri(Matlab.java:15)
    at demo.tttt$1.mouseClicked(tttt.java:45)
    at java.desktop/java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:278)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6629)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389)
    at java.desktop/java.awt.Component.processEvent(Component.java:6391)
    at java.desktop/java.awt.Container.processEvent(Container.java:2266)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4584)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:746)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:744)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Caused by: java.lang.IllegalAccessException: class com.mathworks.toolbox.javabuilder.internal.MCRThreadUtilities cannot access class sun.awt.AWTAutoShutdown (in module java.desktop) because module java.desktop does not export sun.awt to unnamed module @37574691
    at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:392)
    at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:674)
    at java.base/java.lang.reflect.Method.invoke(Method.java:560)
    at com.mathworks.toolbox.javabuilder.internal.MCRThreadUtilities.runBackgroundTask(MCRThreadUtilities.java:227)
    at com.mathworks.toolbox.javabuilder.internal.MCRThreadUtilities.runWhilePumpingEventQueue(MCRThreadUtilities.java:52)
    at com.mathworks.toolbox.javabuilder.internal.MWMCR$4.invoke(MWMCR.java:835)

后来我发现,如果我将

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    aaa frame = new aaa();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

中的

aaa frame = new aaa();
frame.setVisible(true);

放到外面,就可以运行了,但是我想将运行matlab代码的那串代码放在鼠标监听器里,又会继续报上面的错误

  • 写回答

2条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-07-09 14:56
    关注

    引自免费微信小程序:皆我百晓生

    这个问题看起来是由于Java的安全机制导致的,特别是在涉及到Java的AWT和Java的模块化系统时。当你尝试在Java中调用MATLAB的代码时,似乎MATLAB的代码尝试访问某些Java的内部类(在这种情况下是sun.awt.AWTAutoShutdown),但由于安全限制,它无法这样做。这就是为什么你在构造函数内运行代码没有问题,但在监听器内运行相同的代码却引发错误的原因。

    解决这个问题的一种可能的方法是尝试绕过Java的安全机制,但这通常不推荐,因为它可能会导致未定义的行为和其他潜在问题。然而,在这种情况下,你可以尝试以下解决方案:

    1. 修改Java的模块化系统配置:你可以尝试修改Java的模块化系统配置来允许MATLAB的代码访问所需的内部类。这可能涉及到编辑Java的模块信息文件(module-info.java)或JVM启动参数来更改模块访问权限。但是,这种方法可能需要对Java和模块化系统的深入了解。
    2. 将MATLAB代码移到外部进程:考虑到在JFrame中运行MATLAB代码引发了问题,你可以考虑将MATLAB代码作为一个独立的进程运行,并通过Java程序与其进行通信。这样,MATLAB代码不会直接尝试访问Java的内部类,从而避免了这个问题。你可以使用进程间通信(IPC)机制(如Socket通信)来实现Java和MATLAB之间的数据交换。

    另外,你的代码中存在一个可能的逻辑问题。你提到在构造方法中运行代码没有问题,但在鼠标监听器中运行会引发错误。如果aaa frame = new aaa(); frame.setVisible(true);这段代码是在构造方法中执行的,那么当构造方法执行完毕并返回后,JFrame对象已经被创建并显示。如果你在鼠标监听器中再次尝试创建和显示JFrame对象,这看起来是不必要的,因为JFrame已经在应用程序启动时创建和显示了。你应该确保你的代码中不会出现重复创建和显示同一个JFrame对象的逻辑错误。

    希望这些信息能对你有所帮助!如果你有更多关于这个问题的细节或需要进一步的帮助,请随时向我询问。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 7月9日

悬赏问题

  • ¥15 微信小程序跳转关联公众号
  • ¥15 Java AES 算法 加密采用24位向量报错如何处理?
  • ¥15 使用X11可以找到托盘句柄,监控到窗口点击事件但是如何在监听的同时获取托盘中应用的上下文菜单句柄
  • ¥45 字符串操作——数组越界问题
  • ¥15 Loss下降到0.08时不在下降调整学习率也没用
  • ¥15 QT+FFmpeg使用GPU加速解码
  • ¥15 为什么投影机用酷喵播放电影放一段时间就播放不下去了?提示发生未知故障,有什么解决办法吗?
  • ¥15 来个会搭建付费网站的有偿
  • ¥100 有能够实现人机模式的c/c++代码,有图片背景等,能够直接进行游戏
  • ¥15 以AT89C51单片机芯片为核心来制作一个简易计算器,外部由4*4矩阵键盘和一个LCD1602字符型液晶显示屏构成,内部由一块AT89C51单片机构成,通过软件编程可实现简单加减乘除。