开发者

Java programs on Windows, Mac and Linux (Unix)

开发者 https://www.devze.com 2023-03-10 21:42 出处:网络
I am working on a Java app which is of course supposed to run on the major operating systems from a usb stick. For the Mac users I bundled the jars (App and updater) into Apple .app programs. Now: wha

I am working on a Java app which is of course supposed to run on the major operating systems from a usb stick. For the Mac users I bundled the jars (App and updater) into Apple .app programs. Now: what is the best way to let the jars launch each other and use the开发者_如何学Python relative folder path on the stick. I am asking because I noticed that on a Mac it is like the .app folder is the file launched (and not the jar inside) and thus the parent dir is the working directory where all my files will be stored. On Windows and Linux it is just some MyApp.app folder and the files will be inside. Is case and path switching by code really the only way to solve this? Thank you


You can ask the JVM for the physical location of a given class. From http://www.exampledepot.com/egs/java.lang/ClassOrigin.html:

// Get the location of this class
Class cls = this.getClass();
ProtectionDomain pDomain = cls.getProtectionDomain();
CodeSource cSource = pDomain.getCodeSource();
URL loc = cSource.getLocation();  // file:/c:/almanac14/examples/

This works even for jar-files.

When you know the location, you can navigate from there.


The best option would be to create launchers for each of the OSes and from these launchers call the JVM to run jar's. For Linux make a shell script, on Mac it would be .app (or shell script too) and on Windows a .bat or an executable would work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号