0

I've created a desktop app using Java AWT. I can run the app successfully from the eclipse, but when I exported the runnable jar file and try to run it, it gave me an error and not working. Here is the error:

***WARNING: Display must be created on main thread due to Cocoa restrictions. Use vmarg -XstartOnFirstThread
org.eclipse.swt.SWTException: Invalid thread access
    at org.eclipse.swt.SWT.error(SWT.java:4723)
    at org.eclipse.swt.SWT.error(SWT.java:4638)
    at org.eclipse.swt.SWT.error(SWT.java:4609)
    at org.eclipse.swt.widgets.Display.error(Display.java:1103)
    at org.eclipse.swt.widgets.Display.createDisplay(Display.java:851)
    at org.eclipse.swt.widgets.Display.create(Display.java:835)
    at org.eclipse.swt.graphics.Device.<init>(Device.java:132)
    at org.eclipse.swt.widgets.Display.<init>(Display.java:734)
    at org.eclipse.swt.widgets.Display.<init>(Display.java:725)
    at org.eclipse.swt.widgets.Display.getDefault(Display.java:1437)
    at MainWindow.open(MainWindow.java:74)
    at MainWindow.main(MainWindow.java:64)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:61)

Thanks for any reply.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ali
  • 66
  • 6
  • Have you tried running it with the argument that the error message is telling you to use? `java -XstartOnFirstThread -jar YourJarFile.jar` or something like that? – JustAnotherDeveloper Sep 07 '20 at 09:02
  • Thank you for your quick reply. I tested it with the command, and it works. Now, how can I make it run automatically, so the user can double click and run it? – Ali Sep 07 '20 at 09:13
  • I guess one easy way is that you could provide a script that runs that command and instruct the user to run the script, not the jar. – JustAnotherDeveloper Sep 07 '20 at 09:15
  • I am getting this error in windows, while in mac it works prefectly. at java.base/java.lang.reflect.Method.invoke(Method.java:564) at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:61) Caused by: java.lang.UnsatisfiedLinkError: Could not load SWT library. Reasons: no swt-cocoa-4934r6 in java.library.path: [C:\Program Files\Java\jdk-14.0.1\bin, C:\WINDOWS\Sun\Java\bin, C:\WINDOWS\system32, C:\WINDOWS, C:\Program Files\Common Files\Oracle\Java\javapath, C:\Program Files (x86)\Common Files\Oracle\Java\javapath, C:\Python38\Scripts\, – Ali Sep 07 '20 at 10:06
  • The intial question is potentially a duplicate of https://stackoverflow.com/q/3976342; maybe also have a look at https://www.eclipse.org/swt/macosx/ – Marcono1234 Sep 07 '20 at 10:55
  • None of the suggested links fixes the problem. My case is that I could run the jar file on a mac using the command: java -XstartOnFirstThread -jar YourJarFile.jar While in windows, I couldn't get it run. When I applied the same command, it gave me: Unrecognized option: -XstartOnFirstThread Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit. If I run it with the option "XstartOnFirstThread" it will not work. – Ali Sep 07 '20 at 11:46
  • If it's AWT as tagged and mentioned in the question, why is the stack trace referring to SWT? – Andrew Thompson Sep 17 '20 at 09:18

0 Answers0