简体   繁体   English

使用JarBundler将Java转换为MacOSX的.app文件

[英]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. 我所要做的就是将一个简单的java程序转换为macOSX .app文件,这样我就可以将它作为一个简单的可执行文件分发。

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 使用Xcode - 据我所知,Xcode不再支持Java Build
  2. Use Jar Bundler - All the tutorials seem to be out of date, and it doesn't work. 使用Jar Bundler - 所有教程似乎都已过时,但它不起作用。 When I run: java -jar jarbundler-2.0.0.jar 当我运行:java -jar jarbundler-2.0.0.jar

    The console returns "no main manifest attribute in jarbundler-2.0.0.jar" 控制台返回“jarbundler-2.0.0.jar中没有主要清单属性”

    If I'm right, this would be a fail on whoever packaged the jar file right? 如果我是对的,那么打包jar文件的人会失败吗? 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 他们说你可以自己创建目录和文件,然后将你的文件夹重命名为.app,然后对它运行一个命令,但这很复杂,因为我不确切知道如何创建一个info.plist文件......如果你想这样做,我到目前为止找到的最好的网站就是这个: OSX的Java部署选项

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. 我已将我的代码编译成.jar文件,我有一个图标和所有内容,我需要做的就是以某种方式将其打包到.app文件中。

Thanks for the help in advance! 我在这里先向您的帮助表示感谢!

Solution

EDIT: I got it! 编辑:我明白了! After 7 or 8 hours of searching and reading it works. 经过7或8个小时的搜索和阅读后,它可以工作。 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. 我放弃了自己做的方法,然后回到了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. 如果其他人正在读这个并且他们对ANT一无所知,那就是我所做的:下载JarBundler,我从这里得到了我的: Mac OS X JarBundler ANT任务并按照其中的说明操作。

When you download it, put the jarbundler-2.2.0.jar file here: 下载时,请将jarbundler-2.2.0.jar文件放在此处:

/usr/share/ant/lib 的/ usr /共享/蚂蚁/ 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 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. 确保你移动了正确的jarbundler,这是我遇到问题的地方,我移动了整个文件夹,而不是内部.jar文件,所以请确保你移动你下载的文件夹zip里面的.jar,而不是整个东西,否则当你去构建它时,你会得到一个错误,如错误:taskdef class net.sourceforge.jarbundler.JarBundler找不到。

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文件(有两种类型,一种是像.zip,另一种是可执行文件)你想要一个普通的JAR文件,不是可执行的,所以要做到这一点你首先需要编译您的代码获取.class文件,然后打开命令提示符并按照此处的说明操作: 创建一个Jar文件该命令将如下所示:

jar cf myName.jar *.class 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. 获得jar文件后,需要在同一文件夹中创建.xml文件,并且需要将其命名为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: 从那里(顺便说一下,我使用的是Macbook Pro)你所要做的就是从该文件夹运行一个命令行:

ant bundle 蚂蚁束

and it should create a .app file in a folder called release. 它应该在名为release的文件夹中创建一个.app文件。 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) 总结一下:你需要的只是同一个文件夹中的.jar文件和build.xml文件,然后运行命令:ant bundle(或者你在build.xml文件中命名为target的任何东西,我把它命名为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! 如果有人想知道,我拿了由JarBundler生成的Info.plist并将其放入我正在制作的目录中并且它有效! So that was the problem, I just didn't know how to format it right... here it is: Info.plist 所以这就是问题,我只是不知道如何正确格式化......这里是: Info.plist

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. 在Mac OS X上, 应用程序包是一个名称以.app结尾的目录,其中包含应用程序使用的资源。 This game is an example that constructs the bundle in situ using an ant build script . 这个游戏是一个使用ant 构建脚本 在原位构建bundle的示例。 Additional references are cited here . 这里引用其他参考文献。

If you want to use Oracle Java instead of Apple's discontinued Java, you should use Java Application Bundler instead of JarBundler. 如果要使用Oracle Java而不是Apple已停产的Java,则应使用Java Application Bundler而不是JarBundler。

See also these answers: 另见这些答案:

Worked for me! 为我工作! For some reason I couldn't put the jarbundler .jar file in the Ant area, but instead had to point at it explicitly from the build.xml (I put it in <projectRoot>/jarbundler/jarbundler-2.2.0.jar ). 出于某种原因,我无法将jarbundler .jar文件放在Ant区域中,而是必须从build.xml中明确指出它(我把它放在<projectRoot>/jarbundler/jarbundler-2.2.0.jar ) 。 My build.xml looked like: 我的build.xml看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<project name="TripleWhammy" default="default" basedir=".">        
    <taskdef name="jarbundler"
             classname="net.sourceforge.jarbundler.JarBundler"
             classpath="jarbundler/jarbundler-2.2.0.jar" />

    <target name="bundle">
        <jarbundler dir="."
                    name="Triple Whammy"
                    mainclass="org.bigclue.TripleWhammy"
                    jar="target/triple-whammy-1.0-jar-with-dependencies.jar"
                    icon="triple-whammy.icns"/>
    </target>
</project>

This created "Triple Whammy.app" in my project directory. 这在我的项目目录中创建了“Triple Whammy.app”。 My icon looks good. 我的图标看起来不错。 Sweet. 甜。

Now that I've built a simple distributable dice .app , time to build something awesome... 现在我已经构建了一个简单的可分发骰子.app ,时间来构建一些很棒的东西......

(Mac OS X 10.6.8, Java 1.6.0_51, Ant 1.8.4; built my .jar using Maven 3.0.4 and its "assembly" plugin version 2.4). (Mac OS X 10.6.8,Java 1.6.0_51,Ant 1.8.4;使用Maven 3.0.4构建我的.jar及其“汇编”插件版本2.4)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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