简体   繁体   中英

File to open terminal and start .jar in OSX?

I currently have a small text game I've written in Java that utilizes System.out.print(); to output text and a Scanner to receive input from the user.

I've compiled the runnable .jar and used IExpress to create a .exe that runs the batch command java -jar "foo.jar" upon execution. It's not the best way to distribute the game, but it's a quick way to distribute it to friends for play testing in Windows.

I'm wondering if there is an equivalent for this for OSX? I know I can write a shell script to open the terminal and execute the .jar file, but will it work if the shell script and .jar are distributed to other computers? Does each person have to give the .sh file "execute" permission? Some of these people aren't really tech savvy and even mentioning opening terminal would turn them off to helping me play test.

So is there a way to give them something to double-click on that will open terminal and run my .jar?

Thanks in advance!

Here are two possible options (the first of which will probably not open the terminal, but I'm leaving here in case you find a way to do so with the below mentioned JarBundler)

1 - Building a native java application bundle and installer

The answer is somewhat two-fold. OSX applications have a specific directory structure which the operating system expects to be in place, they are called application bundles (google for "osx application bundle programmers guide" for the messy intricate details). Jar Bundler is an Ant task which can build a bundle for you without you needing to understand the bundle format itself.

That alone is enough to allow you to drag the application onto the OSX dock. However, what is more common is an installer. To build an installer, you need to build a DMG file. Some good documentation on how to create this with ant can be found here .

The gist of it is to create a disk image, attach to it, copy your application into it, detach from it, and compress it. A typical OSX installer is a window with a copy of your application and a symbolic link to the /Applications directory. When the installer window opens up, it displays both of these items and you normally drag the application to the /Applications icon, "installing it" (in reality, just copying it to /Applications).

The following the relevant Ant build files from my project for building an application bundle as well as building a DMG file (both are google-code links). They do not require any of the GUI tools.

Both of those files are pretty heavily commented.

However, I have a feeling this will not open the terminal window as you desire (you may be able to work with the stubfile option in JarBunder to get it to do what you want).

2 - Create an application bundle from your shell script

This link shows you how to create an application bundle out of a shell script.

You can then create a DMG file out of your application bundle to simplify installation for your users. The DMG file will preserve the execution file attributes so they will not need to manually chmod +x your script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM