Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
701 views
in Technique[技术] by (71.8m points)

macos - Convert Java to .app file for MacOSX using JarBundler

All I'm trying to do is convert a simple java program into a macOSX .app file so I can distribute it as a simple executable file.

I've read all over the place and everyone says the same things:

  1. Use Xcode - As far as I can tell, Xcode no longer supports Java Builds
  2. Use Jar Bundler - All the tutorials seem to be out of date, and it doesn't work. When I run: java -jar jarbundler-2.0.0.jar

    The console returns "no main manifest attribute in jarbundler-2.0.0.jar"

    If I'm right, this would be a fail on whoever packaged the jar file right? But I've tried downloading it from other websites and other versions, but I get the same thing.

  3. They say you can just create the directories and files yourself and then rename your folder to .app and then run a command on it or something but this is complicated since I don't know exactly how to create an info.plist file...if you want to do it this way, the best website I've found so far is this one: Java Deployment Options for OSX

I've compiled my code into a .jar file, I have an icon and everything, all I need to do is package it somehow into a .app file.

Thanks for the help in advance!

Solution

EDIT: I got it! After 7 or 8 hours of searching and reading it works. Since I'm new to this, I'm going to post everything I learned to save others that run into the same problem from going through the pain. I gave up on the do it yourself method and went back to JarBundler. If anyone else is reading this and they don't know anything about ANT, here's what I did: Download JarBundler, I got mine from here: Mac OS X JarBundler ANT Task and follow the instructions there.

When you download it, put the jarbundler-2.2.0.jar file here:

/usr/share/ant/lib

you might have to run a command to move it, something like:

sudo mv jarbundler-2.2.0.jar ~/../../usr/share/ant/lib/jarbundler-2.2.0.jar

like I said, I'm still learning, but this worked for me.

Make sure you moved the right jarbundler, this is where I ran into problems, I moved the entire folder, rather than the inner .jar file, so make sure you move the .jar that's inside the folder zip you downloaded, not the whole thing, otherwise when you go to build it, you'll get an error saying something like error: taskdef class net.sourceforge.jarbundler.JarBundler could not be found.

Then you need to compile your code into a .jar file (there are two types, one is like a .zip and the other is an executable) You want a normal JAR file, NOT executable, so to do that you first need to compile your code to get .class files, then open a command prompt and follow the instructions here: Create a Jar File The command will look something like this:

jar cf myName.jar *.class

Once you have your jar file, you need to create a .xml file in the same folder and needs to be called build.xml. Edit it with a text editor. Follow the instructions on the link above to create it. It will look something like this:

//build.xml

<project name="MyProject">

<taskdef name="jarbundler"
classname="net.sourceforge.jarbundler.JarBundler" />

<target name="bundle">

<jarbundler dir="release"
        name="Apollo's Image Editor"
        mainclass="ImageEditorGUI" 
        jar="ApolloIE.jar"
        icon="Aperture.icns" />

</target>

</project>

From there (I'm using a Macbook Pro by the way) all you have to do is run a command line from that folder:

ant bundle

and it should create a .app file in a folder called release. So to summarize: all you need is your .jar file and build.xml file in the same folder, and then run the command: ant bundle (or whatever you named your target in the build.xml file, I named it bundle)

Good luck, hope this helps anyone out there.

If anyone is wondering, I took the Info.plist that was generated by the JarBundler and put it into the directory I was making and it worked! So that was the problem, I just didn't know how to format it right... here it is: Info.plist

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

On Mac OS X, an application bundle is a directory that has a name ending in .app and that contains the resources used by the application. This game is an example that constructs the bundle in situ using an ant build script. Additional references are cited here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...