我将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代码的那串代码放在鼠标监听器里,又会继续报上面的错误