0

I just finished coding a new Java application, and I would like to export it to a format that can be read on both Windows and Mac (or, if needed, two separate formats). I tried right-clicking on my project and exporting to a runnable JAR file, choosing to extract the required libraries into the generated JAR.

Since I've never exported from Eclipse before (I'm used to Unity and, to a lesser extent, Visual Studio), I'm sure I did something wrong, as it would not open on either macOS Sierra or Windows 10. What did I do wrong? Thanks!

EDIT: Here's the output when I try to run java -jar on the JAR (file name has been obfuscated):

***WARNING: Display must be created on main thread due to Cocoa restrictions.
Exception in thread "main" org.eclipse.swt.SWTException: Invalid thread access
    at org.eclipse.swt.SWT.error(SWT.java:4533)
    at org.eclipse.swt.SWT.error(SWT.java:4448)
    at org.eclipse.swt.SWT.error(SWT.java:4419)
    at org.eclipse.swt.widgets.Display.error(Display.java:1089)
    at org.eclipse.swt.widgets.Display.createDisplay(Display.java:840)
    at org.eclipse.swt.widgets.Display.create(Display.java:823)
    at org.eclipse.swt.graphics.Device.<init>(Device.java:129)
    at org.eclipse.swt.widgets.Display.<init>(Display.java:722)
    at org.eclipse.swt.widgets.Display.<init>(Display.java:713)
    at org.eclipse.swt.widgets.Display.getDefault(Display.java:1419)
    at myJar.MainWindow.main(MainWindow.java:19)
Tyll'a
  • 526
  • 1
  • 5
  • 17
  • How are you trying to "open it"? Does it run if you execute `java -jar `? – Elliott Frisch Mar 14 '17 at 03:02
  • JAR is portable between both Mac and Windows (and Linux), you can use a native wrapper to generate a .exe (on Windows) which will execute your Jar, some are light wrappers, requiring you to deploy both the jar and exe file, some are fat, which embed the jar within them. You can also pack the app into a Mac App bundle as well, depending on your needs – MadProgrammer Mar 14 '17 at 03:04
  • MadProgrammer, that's why I thought a JAR file would be best. – Tyll'a Mar 14 '17 at 03:06
  • 1
    SWT uses native components. – Elliott Frisch Mar 14 '17 at 03:06
  • Okay, now I'm confused. Do I have to create a Mac executable on the Mac partition and then go over to the Windows partition and create a Windows executable? Like I said, I'm still somewhat new to Eclipse. – Tyll'a Mar 14 '17 at 03:07
  • @PSDuckie No, not really, it makes it easier for users to integrate on their systems, but it's not required. SWT uses native bindings into the system, so it's using native UI controls based on the platform it's using. Your problem is you've violated the thread rules of SWT. [This](http://stackoverflow.com/questions/3976342/running-swt-based-cross-platform-jar-properly-on-a-mac) might explain the reason for the error and [this](http://www.eclipse.org/swt/faq.php#carbonapp) may also help – MadProgrammer Mar 14 '17 at 03:15
  • You didn't include swing libs in your package i think. I did same recently with javafx and i am macuser ..jar was executable (double click,) – minigeek Mar 14 '17 at 03:25

1 Answers1

0

You appear to be running a SWT application. On macOS you must specify the -XstartOnFirstThread option when running the jar to run SWT correctly:

jar -XstartOnFirstThread -jar xxxx.jar

SWT applications also have a native library which is different for each platform.

greg-449
  • 109,219
  • 232
  • 102
  • 145