Compile Java To Mac App

‎Pico Compiler is a lightweight development environment for writing Java programs. Key features:. Java 9 offline compiler. Developer keyboard - Developer keyboard adds a new row of keys to the default on-screen keyboard. It consists of swipe buttons with all common symbols, punctuation and brack. Jan 08, 2020  This wikiHow teaches you how to turn your Java source code into an executable app using a local and online compiler. If you're using a computer, the most common way to compile Java code is using Java Software Development Kit (Java SDK) from the command line. Aug 28, 2012  Change to the directory of your personal java files. For example, I would input cd java since that is the personal java folder I created above. Now our current directory within the CMD shell is c:java. Type in javac JavaFileName.java where the “JavaFileName.java” is the name of the java file you want to compile. This file can be. Valid Java classes can be compiled into bytecode. You can compile and run classes with the main method right from the editor using the green arrow icon in the gutter. Click in the gutter and select Run 'HelloWorld.main' in the popup. The IDE starts compiling your code. Sep 29, 2016  This app is ideal for learning and testing code snippets! Java is a general-purpose programming language that is class-based and object-oriented. It is intended to let application developers write once, run anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.

In the past two articles you have seen how to customize your Java application so that it looks and feels more like a native Macintosh application when running on Mac OS X without changing the end user experience on other platforms. A combination of runtime properties and coding changes that targeted Mac OS X specific APIs made a big difference to that audience.

Recall that Mac OS X is a melding of two worlds. Hard core UNIX programmers can pop open a Terminal window and write their Java code using vi and compile and run it from the command line. There is, however, the more traditional Mac audience that interacts with their computer through a friendly UI that follows Apple Human Interface guidelines.

In this article, we look at deploying your Java application. The technical geek audience might be happy with running a class with a main() method from the command line but the wider audience expects a double-clickable icon that looks and acts like every other native application. In this article, we travel from one end of the spectrum to the other to broaden your potential user base.

Although you should 'test everywhere', your build machine may not be a Mac. Fortunately, as you will see, a double-clickable Macintosh application is just a directory with some special contents and a name that ends with .app. Even on a Windows machine you should be able to modify your build script to package up a Mac-specific version of your application.

Primitive Distributions

Because Mac OS X ships with J2SE 1.4.1 and J2SE 1.3.1, you can distribute your application as class files or jar files and - in theory - your customer could run your application from the Terminal application. We start with these models and quickly move to double-clickable jar files and shell scripts.

For this article, use the Java Sound Demo as the running example. Download and unzip the zip file. Inside the JavaSoundDemo directory you will find the source files inside of the src subdirectory, a jar file, audio files, and html files that we will not use.

Raw Class Files

As a developer, you don't think twice about compiling the source files and running the application using the command line. Compiling the eight files in the src directory generates fifty class files. You can then run the sample application from the command line like this.

java JavaSound

The Java Sound Demo starts up. We haven't customized the application in any way so the menu appears at the top of the JFrame and not where Macintosh users expect. The application looks like this out of the box.

You have done this compile and run step so many times that you hardly think twice about it. Think of the least technical person you know and ask whether they would be likely to follow these steps to run your application if a competing application were easier to install and run.

This example demonstrates two separate areas of usability. Once we got the application up and running it looked good and ran fine. You would not, however, want to distribute an application to an end user this way. You would have to somehow bundle up the fifty class files for easy download and installation. You would then have to provide instructions for running the application using, in the case of Mac OS X, the Terminal application.

Jar Files

If you are going to have to package up the class files for distribution anyways, you may as well produce a jar file. And, if you are going to produce a jar file, it ought to be executable. In the case of the Java Sound Demo, the file JavaSoundDemo.jar is executable. Because Mac OS X ships with the Jar Launcher application, the end user needs only double click on the jar file and the application will launch.

To make the jar file executable, the manifest must include the name of the Main class file. Unjar JavaSoundDemo.jar with the command jar xvj JavaSoundDemo.jar. Here's the file META-INF/MANIFEST.MF.

Shell Scripts and Helper Applications

Class

For larger or more complicated applications you are likely to have more than one jar file along with resource files. A common strategy for targeting multiple platforms is to include a batch file and a shell script. Choose the non-platform specific download from the NetBeans homepage. Inside of the bin subdirectory you will find applications for running NetBeans on a variety of platforms.

The shell script runide.sh can be run from the command line like this.

Compiling Java On Mac

sh runide.sh -jdkhome /Library/Java/Home

The NetBeans IDE starts up with this decidedly non-Mac OS X look and feel.

You could, of course, modify the shell script to modify this look and feel, but the NetBeans developers decided on a different approach. Even though the typical NetBeans audience member is technically competent, there should be a friendlier way to start the IDE. They have created a native Mac OS X application called NetBeansLauncher.

The version of NetBeansLauncher that is included in the generic NetBeans download is a good next step. You will see how the team took it farther in the next section. On a Mac OS X computer you can double click on the macosx_launcher.dmg file inside of the bin directory. This is a disk image. Drag the NetBeansLauncher from the expanded disk image back into the bin directory. Now double click on the NetBeansLauncher. The ReadMe file that was also in the disk image provides the following information about usage.

When launched for the first time, NetBeansLauncher needs to find NetBeans root directory. First it looks into NetBeansLauncher.app itself. If it does not find NetBeans root directory there user must specify NetBeans root directory manually.

For this download, the first time the user starts up the NetBeansLauncher, they need to navigate to the netbeans directory. After that, double clicking on the NetBeansLauncher starts up the NetBeans IDE as if it were any other native Mac OS X application.

First Class Mac OS X Applications

If you download the Mac OS X disk image from the NetBeans distribution and mount it you may be surprised at the simplicity of what you find. Unlike the complex structure visible in the other distributions, you will see five files with documentation and a single application. To install, you can move this NetBeansLauncher application anywhere on your hard drive. Double click on it and the NetBeans IDE starts right up.

This is the experience that is expected on Mac OS X. The package structure and complexity is hidden from the user and they can't accidentally move a file that renders the IDE unusable. In this section we'll look more closely at the package structure and how to create a Mac OS X application whether or not our build machine is a Mac.

Packages in Mac OS X

Consider again the sentence from the NetBeansLauncher instructions that says in order to locate the NetBeans root directory, 'First it looks into NetBeansLauncher.app itself.' This implies that NetBeansLauncher is a directory with the name 'NetBeansLauncher.app'. In the mounted disk image, either right click or Ctrl-click on the NetBeansLauncher icon and choose to 'Show Package Contents'.

The structure is the same for all Mac OS X applications. There is a Contents directory with an XML file named info.plist, a text file named PkgInfo, a MacOS directory, and a Resources directory. If you don't have a creator code registered with Apple the PkgInfo text file should contain only the following.

If you have a creator code, use it in place of the question marks. Ordinarily the MacOS folder contains a small stub file that launches the Java VM. In this case the NetBeans team has written their own application. We will look more closely at a property list file in the next section. For now, take a look at the contents of the Resources directory.

You can see the contents of the same netbeans package inside of the Resources subdirectory. This is your key to deploying on Mac OS X. Add in the necessary pieces and then just bundle up your ordinary distribution in the appropriate location. If you have a more flexible build process you should also strip out those pieces that aren't needed for the Mac OS X application such as the Windows executables.

If you are interested in digging deeper into the structure of a Mac OS X application, you will find more information in the Apple publication Anatomy of a Bundle.

Creating 'Native' Java Applications on Mac OS X

If you develop on Mac OS X you can use the Jar Bundler application to turn jar files into Mac OS X applications. Jar Bundler is distributed with the other developer tools and is located in Developer/Applications/. Start it, select the 'Classpath and Files' tab and add the file JavaSoundDemo.jar.

Compile Java To Mac App

Select the 'Build Information' tab. For 'Main Class', navigate to the JavaSoundDemo.jar file again and select JavaSound from the drop down list. This list is populated by any classes in the jar file containing a main() method. Accept all of the default settings for the options. You can use the default Java application icon or you can create your own. The icon you see below started as a screen shot of the running Java Sound Demo and was transformed into an icon using the IconComposer application that is also distributed as part of the developer tools.

Press 'Create Application' and enter the name 'JavaSoundDemo'. A Mac OS X application is created for you. You can show the package contents of the generated application as before. You can view the property list with Apple's Property List Editor or with any text editor. It is just an XML file with properties stored as name - value pairs.

The Java properties indicate the location of the jar file, the name of the Main class, and the version of the JRE to be used. The other properties include a pointer to the icon file and to the Java application stub file that is the native executable.

Creating Mac OS X Java Applications on Other Platforms

Take a look at the contents of the package that was generated by Jar Bundler on Mac OS X.

On another platform you need to duplicate this structure. To create an application named 'JavaSoundDemo' on, say, a Windows machine, start by creating a directory and naming it JavaSoundDemo.app. Next, create a subdirectory named Contents. Inside of Contents you will need a MacOS directory with the JavaAPplicationStub. You can create the PkgInfo text file and your Info.plist can also be generated by hand and should contain the following XML.

You will need a Resources directory with a Java subdirectory into which you put the JavaSoundDemo.jar file. In other words, with the exception of the JavaApplicationStub and the music.icns file, everything else can be created on another platform.

If you use Ant you can easily add a target that takes your jar files, images, and other resources and bundles it up as a Mac OS X application bundle that includes the plist file and Java application stub in the appropriate locations. Every time you create a new build you will automatically have your Mac OS X version. You can also find a growing number of Ant tasks that automate some of the steps outlined in this article.

Summary

When it comes time to deploy your Java application, consider the ease of use of your target audience. Even developers appreciate the double clickable version of the NetBeans IDE. Creating a Java application that looks and feels like a native application does not require a lot of extra work and can easily be integrated into your build process even if your build machine runs a different operating system.

For More Information

by Adrian D. Finlay

Did you know that you can use Java to make cross platform mobile apps? Yes, pinch yourself, you read that right the first time! I’ll teach you the basics of how to use your existing Java knowledge to create performant apps on Android and iOS in 12 easy steps. We will do this all using JavaFX as the GUI toolkit.

But first, some more foreground. You will need to meet the subsequent requirements to be able to build an application for both Android and iOS. However, if you do not desire to build an iOS application, you can feel free to develop on any x64 bit machine that supports Java SE 8. This project will be a Git repository built with gradle. But you do not need to create a Git repository.

The following are the requirements:

  • A JDK 1.8 Compliant JVM
  • Android Command Line Tools (SDK v.27)
  • XCode 9.2
  • Gradle 4.2
  • Git Large File Storage (v.2.5.0) (Unnecessary if you do not want to create a git repository)
  • Preferably at least 4G RAM

Impatient? Want to see an end result? Check out the completed project below.

afinlay5/OnyxFx
Gradle source code repository for OnyxFx, a cross-platform (Android/iOS/Linux/macOS/Windows) JavaFX app rendering…github.com

My development environment will be Fedora Linux 28 and macOS High Sierra. Now that we’ve got that out of the way, let’s dig in.

1) Create a folder to house the project

I hosted my project, OnyxFx, as follows: “/home/adriandavid/Projects/OnyxFx”. You are, of course, free to host the project wherever you please.

2) Initialize gradle, Git, set JAVA_HOME

Open a terminal in the root of the project directory. If gradle is properly configured, you should see something like this after executing the following command:

You need to make sure that gradle lists your Java Development Kit (JDK) 8 installation adjacent to the section labeled “JVM”.

While there are many ways to do this, the most straightforward way is to ensure that your JAVA_HOME environmental variable is properly set.

Depending on your environment, there are many ways to do this. One way to do this in most *nix environments is to set the variable in /home/<user>/.bashrc or /etc/profile. See the manual for your operating system to ensure that your JAVA_HOME environmental variable is set correctly.

You can include the following lines at the end of either .bashrc or profile to ensure that JAVA_HOME is set correctly.

Note: You may install Oracle’s JDK 8 here.

Then, make sure that the shell reflects the above changes by running one of the following commands:

Enter the following command to verify that the variable is correctly set:

If you are still experiencing difficulty or you are using Microsoft Windows, see here.

First, run git init in the project’s root directory to initialize the Git repository. Note: should you not desire to host a git repository, you may skip this step.

Second, run gradle init in the project’s root directory to initialize the gradle repository. This step is required.

Note: You will notice that my example appears slightly different. This is because I already have gradle and Git initialized on my local environment.

3) Get groovy! Edit gradle.build and

Hopefully Earth, Wind, & Fire can help you get groovy! Power up your favorite text editor, and edit your build.gradle located in your project’s root directory and replace the contents with the contents of the following GitHub gist.

These build.gradle settings configure our gradle project to use the javafxmobile plugin, which is the work horse of our project. You can learn more about the plugin here and here. Among many things, the javafxmobile plugin automates the process of downloading (from Maven Central or jcenter) and adding the iOS and Android SDKs to your application’s classpath.

If you are familiar with gradle, maven, or ant, great — you probably have an idea of what’s going on. If you are not familiar with gradle, don’t worry about it. All you need to understand is that gradle is a build tool used to automate many tasks involved in building apps such as: grabbing dependencies, project organization, and so on.

Notice that we are targeting Android 7.1 Nougat (API version 25) and iOS 11 (we will see where this is done shortly). You may adjust these values as you see fit. Note, however, that in the case of Android, you must ensure that the API version matches the version of the SDK that you have download (more on this later).

Lastly, I will not demonstrate the production of signed executables in this tutorial. For this reason, iOSSkipSigning is set to true and we do not make use of the releaseAndroid gradle task. You can, however, provide the appropriate accommodations to produce signed apps.

4) Make a new file called gradle.properties and configure it

Create a new file in the project’s root directory called gradle.properties and add the following content to the file.

These settings tell the javafxports plugin to use an iPhone-7 as the on-board emulator, to target iOS 11, and to pass the Xms and Xmx flags to the JVM, which specifies both the initial memory pool to 4GB and the maximum heap memory pool to 8GB. This will be necessary for the compilation of the openJDK and the development of the iOS build.

5) Install Homebrew (iOS only)

If you do not have a Mac and are not intending to produce an iOS build, feel free to skip this step.

Open the terminal in macOS and paste the following command.

6) Install the USB Multiplexing Socket (iOS only)

Only move on to this step if Homebrew has successfully installed. If you do not have a Mac and are not intending to produce an iOS build, feel free to skip this step.

Open the terminal in macOS and paste the following command.

7) Grab the Android Command Line Tools

Grab Android Command Line Tools for your platform here. After the download has finished, unzip the folder and paste the contents in the directory of your choice. For me, this was /home/<user>/Android.

8) Set Android_HOME, Grab necessary Android packages

As with Java, gradle needs to know where to look to find the Android Command Line Tools. There are a few ways to do this. However, in the spirit of simplicity and consistency, we will set the ANDROID_HOME environmental variable in this tutorial. Add the following variable in the same way that we did it for JAVA_HOME. For example:

Remember to reload the shell by adding source <file> as we did for JAVA_HOME.

Now, grab the tools necessary to build the Android build. Execute the following command:

Take careful notice that the SDK and API version we have specified in gradle.build correspond to the version we have specified in this command. Namely, “25”. Should this be misaligned, the build will not succeed.

9) Create the application’s directory structure

To automate the process of creating these directories, execute the following shell script.

Bourne-Again Shell / Korn Shell:

Windows Shell (cmd): Best multi monitor software mac.

Save the file as mkpdir.bat or mkpdir.sh and execute the file from the project’s root directory as root (or Administrator).

Notice that we created directories for embedded and desktop. We will produce a desktop build, because it takes no additional work to do so. However, we will not produce any builds for embedded devices.

10) Create your JavaFX Application!

Navigate to /src/<platform>/java and begin developing your JavaFx application! Application resources are stored in /src/<platform>/resources.

You can start with a simple Hello World application or look at the source code that I have put together here. OnyxFx is an application I made, following these instructions, that makes REST calls over HTTP to the OnyxFxAPI. The API, in turn, is a web scraper that will return the statistical data (points per game, rebounds per game, assists per game) for the NBA® player and season the client specifies. It returns the data as JSON, which is then parsed and displayed to the screen of the mobile app. Feel free to edit it!

Keep in mind that although you can share source code, you should include custom edits in each copy of the source, should you want to make device specific changes.

Also note that the underlying compiler (MobiDevelop’s fork of RoboVM) does not fully support all Java 8 APIs. If you look very closely at my source code, you will notice that in the iOS version of the source code, I have removed unsupported API such as java.util.function.BiConsumer and java.util.Map.replace().

11) Create a RAM disk for iOS builds (iOS only)

The compilation process for iOS is very resource-heavy, as the plugin will compile the entire openJDK and other libraries twice to create a fat JAR that it will use to build your application. Therefore, you should preemptively create a RAM disk to accommodate for the memory requirements.

This step, however, is subject to your judgement of your machine’s capabilities. For context, the macOS machine that I used to compile my iOS app has 4GB of DDR2 RAM. I decided to make an 8GB RAM disk. To do so, execute the following command in the terminal.

12) Build and Run your application!

To build your application, execute the gradle wrapper in the root directory from the terminal as follows.

This will produce a desktop application packaged as a JAR with scripts to run the application provided in /build/distributions/<AppName.tar>; and /build/distributions/<AppName.zip>. Should you unzip the directories, you will notice the following structure:

Notice that in /bin there are scripts to execute the application. These scripts rely on preserving the current folder structure. Also notice that is not necessary for you to have tree installed. It is used here simply for illustrative purposes.

There is, additionally, a standalone JAR that you can use to execute the application on any desktop environment supporting JavaFX 8. To run the application, execute one of the following:

View this project’s gradle tasks

You can view this project’s gradle tasks by running the following in the project’s root directory.

To Compile, Run on Desktop

The following command will run your project in the host environment.

You will find a standalone jar in build/libs/<AppName&gt;.jar .

To Compile, Run on Android

You will find two APKs in build/javafxports/android.
The first will be named <AppName&gt;.apk.
The second will be named <AppName>-unaligned.apk.

To Compile, Run on iOS

You will find three executables in build/javafxports/ios.
The first will be named <AppName&gt;.ipa.
The second will be named <AppName>.dSYM.
The third will be named &lt;AppName>.app.

Some screenshots of my sample app

On Desktop

On Android

Compile Java To Mac App Version

On iPhone

On iPad

Splash Screen

My Closing Thoughts

javafxports is a promising project that aims to bring JavaFX and the Java SE platform onto mobile and other devices. In a way, the tool parallels Xamarin in its efforts. However, the project has a lot of work left to be done.

For a start, the plugin currently does not fully support Java 8. On Android, it uses retrolambda to handle Java 8 Lambda Expressions & Method References. It technically is up to Java 6. Additional dependencies make it such that you can use Java 8. However, the process is straightforward, the builds work as expected, and the compilation time is not too long.

On iOS, however, the builds are extremely memory-intensive and the compilation process takes a very long time. The following is a snippet of the log for ./gradlew createIpa task.

Compile Java To Exe

In total, the process consumed about over 6GB of RAM on my machine. This is hardly ideal. However, the future is promising. A company called Gluon has developed a high performance, fully modular custom JVM fully supporting Java 9, that you can read more about here.

This article is originally published on the blog section of my homepage, here.

Compile Java On Mac

Resources to explore:

Compile Java To Mac Apps

  • JavaFxMobile Plugin Git Repo: https://github.com/javafxports/javafxmobile-plugin
  • JavaFxPorts Documentation: http://docs.gluonhq.com/javafxports/#_how_it_works
  • JavaFxPorts Homepage: http://javafxports.org/
  • Gluon Documentation: https://gluonhq.com/developers/documentation/
  • Google Groups Page for JavaFxPorts: https://groups.google.com/forum/#!forum/javafxports
  • StackOverflow Page for JavaFxPorts: https://stackoverflow.com/questions/tagged/javafxports
  • Gluon Mobile Pricing/License Options: https://gluonhq.com/products/mobile/buy/