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.
- Compile Java To Mac App Software
- Compile Java To Mac App Free
- Compile Java Mac
- Compile Java File Mac
- Compiling Java On Mac
In this tutorial, we will see how to write, compile and run a java program. I will also cover java syntax, code conventions and several ways to run a java program. For this, open command prompt (cmd) on Windows, if you are Mac OS then open Terminal. To compile the program, type the following command and hit enter. Javac FirstJavaProgram.java. Jul 03, 2013 Short video how to compile and execute Java on the command line (terminal) of Apple Mac operating system. 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. JAR files are similar to zip files, but JAR files can have additional attributes that are useful for distributing Java applications. In this exercise, you create an IDE project and then place two pre-written Java source files into that project. Then you will compile the classes and build an executable JAR file. 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.
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
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.sh runide.sh -jdkhome /Library/Java/Home
Compile Java To Mac App Software
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
.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
Before You Begin
Purpose
In this tutorial, you create and build a Java Platform, Standard Edition (Java SE) application using Maven. You install, configure, build, and create an executable Java Archive (JAR) file with Maven.
Time to Complete
Approximately 90 minutes
Background
Maven is a build management tool that is central to project build tasks such as compilation, packaging, and artifact management. Maven uses a strict XML-based rule set to promote consistency while maintaining flexibility. Because most Java-centric continuous integration systems integrate well with Maven, it's a good choice for an underlying build system.The primary goal of Maven is to provide:
- A project model that is reusable, maintainable, and easier to comprehend
- Plug-ins or tools that interact with the declarative model
pom.xml
file. The Project Object Model (POM) is the fundamental unit of the entire Maven system.Maven contains three types of repositories for storing JARs, plug-ins, and other project-related artifacts:
- Local Repository: The location created on your machine when you run your first instance of a Maven command in your machine
- Central Repository: The Maven community-owned repository that contains commonly used libraries
- Remote Repository: A developer-owned custom repository that contains project-related libraries and JARs
What Do You Need?
- Download and install the latest Java SE Development Kit. For this tutorial, the available version is Java SE 8.
- Download the Apache Maven. For this tutorial, the available version is 3.2.2.
Setting Up the Maven Environment
In this section, you extract the downloaded archive and install the latest Maven version to a directory of your choice. You verify the Java installation, set the Maven environment, and verify the Maven installation.
- Verify the Java installation:
java -version
The output displays the Java version that you installed. - Extract the downloaded Maven x.x.x archive to a local directory.The archive names are:
- Windows OS: apache-maven-3.2.2-bin.zip
- Linux OS: apache-maven-3.2.2-bin.tar.gz
- Mac OS: apache-maven-3.2.2-bin.tar.gz
Note: This OBE shows you how to install and create a Java SE application using Maven in a Windows operating system. - Click Start, right-click Computer, and select Properties.
- On the Control Panel home page, click Advanced system settings.
- In the System Properties dialog box, click the Advanced tab, and then click Environment Variables.The Environment Variables dialog box is displayed.
- Click New, add
M2, M2_HOME,
andMAVEN_OPTS
to the environment variables, and click OK. - Under System variables, click New, enter the following values in the Edit System variable dialog box, and click OK:
- Variable name:
Path
- Variable value:
%M2%
(Enter the value afterbin
in the system path)
- Verify the Maven installation:
mvn -version
The output displays the installed Maven version, the Java version, the Java home, the Default locale, and the OS name.
Creating a Java SE Project from a Maven Template
- Open the command prompt, navigate to the directory where you installed Maven, and create
Maven_app,
a Maven-based Java application folder:mvn archetype:generate -DgroupId=com.example.bank
-DartifactId=OracleBanking
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=falseAn archetype is an original pattern/model for creating similar projects. In Maven, an archetype is a template of a project that is combined with user input to produce a working Maven project. The following table describes what the template does:Archetype ArtifactIds Description mvn archetype:generate
Creates a project -DgroupId=com.example.bank
Creates the com.example.bank
dependency package structuremaven-archetype-quickstart
Creates a Java project -DinteractiveMode=false
Sets interactive mode to false
The Java project namedOracleBanking
is created. The following table presents the project details:Project Structure Description OracleBanking
Contains src
folder andpom.xml
src/main/java
Contains Java code files under the package structure
(com.example/bank
)src/test
Contains test code files under the package structure
(com.example/bank
)pom.xml
Contains information about the project and details of various configurations used by Maven to build the project - Open the
OracleBanking
project and verify the Java source file: - Verify Java test file:
AppTest.java
By default, Maven adds theApp.java
source file and theAppTest.java
test file to the default directory structure. - Open the
pom.xml
file and review the code.Each project has a singlepom.xml
file, and eachpom.xml
file has a project element and three mandatory fields:groupId, artifactId,
andversion.
Notice that Maven has already added JUnit as the test framework. The following table describes what each node does:Node Description project
Top-level element in all Maven pom.xml
filesmodelVersion
Object model version that this POM is using groupId
Project groupId (for example, com.example.bank
)artifactId
Project ID (for example, OracleBanking
)packaging
Project files converted into a JAR file version
Project version used in the artifact's repository to separate each version (for example, 1.0-SNAPSHOT
)name
Project display name url
Location of the project site
App.java
Creating and Modifying Java Source Files
In this section, you calculate simple interest by creating the
SimpleInterest.java
source file and modifying the App.java
source file.- Navigate to the directory where you created your Maven project, and then open the specified location:
**Maven_appOracleBankingsrcmainjavacomexamplebank
- Create a Java source file named
SimpleInterest.java.
- Edit the
SimpleInterest.java
file with the following code:ThecalculateSimpleInterest
method calculates the interest rate on the loan amount, the tenure of the loan, and the rate of interest per annum. - Press Ctrl+S and close the file.
- Modify
App.java
with the following code: - Review the code. It should look like the following:
- Press Ctrl+S and close the file.
Compile Java To Mac App Free
Creating a Manifest with Maven
In this section, you learn how to use
The manifest file performs the following tasks:maven-jar-plugin
to create a manifest file and package it by adding it to the JAR file.- Defines the entry point of the project and creates an executable JAR file.
- Adds the class path of the project dependencies.
- Edit the
pom.xml
file:You definedmaven-jar-plugin
inpom.xml,
and configured it within the configuration tag. - Review the code. It should look like the following:In the Maven project, you specify the main class details by updating the
pom.xml
file. Thecom.example.bank.App
class in the project is the main class that will be executed when you execute the JAR file. - Press Ctrl+S and close the file.
You successfully updated your
pom.xml
file.Testing, Building, and Running the Application Using Maven
Testing the Application
In this section, you learn how to test your application with
AppTest.java
using the Maven command-line interface (CLI).- Import the package into
AppTest.java:
- Edit the
AppTest
method: - Review the code. It should look like the following:You modified the simple interest value, and then verified the value by using assert statements in the JUnit test case.
- Press Ctrl+S and then run the test cases in
AppTest.java
inside theOracleBanking
project.mvn test
- Review the output.The test case failed and the assert failed message is displayed.
- Modify
AppTest.java:
You modified the simple interest value, and then verified the value by using assert statements in the JUnit test case. - Press Ctrl+S and then run the test cases in
AppTest.java:
mvn test
The test case executed successfully, and a build success message is displayed.
import junit.framework.Assert;
Building the Application
In this section, you learn how to clean and build your application using the Maven CLI.
- Clean and build your Maven project, and review the output:
mvn clean package
You successfully built theOracleBanking
Java SE application using Maven. - Navigate to the directory where you created
OracleBanking
and notice that atarget
folder was created. - Open the
target
folder, and review the folder structure.Folder name Description classes
Contains .class
files of Java source filestest-classes
Contains .class
files of Java test filesmaven-archiver
Contains the pom.properties
filesurefire-reports
Contains the report of the application when mvn
command is executed.java
Empty Java file OracleBanking-1.0-SNAPSHOT.jar
Contains all the project related details in a single zip file. This is the executable JAR file used to run the application
Packaging and Running the Application
In this section, you learn how to package and run the Java SE project using the Maven CLI.
- Navigate to the directory where you installed Maven, and open the
settings.xml
file.The<localRepository>
tag specifies the local repository location on your machine. By default, the local repository is created in the%USER_HOME%
directory. You can specify another location by updating it in thesettings.xml
file. If you need to set proxy details for the application, then update it in thesettings.xml
file. - Clean and package the files, plug-ins, and libraries before running the application:
mvn clean package
- Use the Maven Local repository to run your Java SE Maven application:
mvn exec:java -Dexec.mainClass='com.example.bank.App' -s '*****location of settings.xml file.********'
- Review the output.You successfully executed the Java SE application named
OracleBanking
using Maven. The simple interest is calculated and displayed in the Maven CLI. - Execute the JAR file with following commands:
cd target
java -jar OracleBanking-1.0-SNAPSHOT.jar
- Review the output.You successfully executed the
OracleBanking
Java SE application by using Maven. The simple interest is calculated and displayed in the Maven CLI.
Want to Learn More?
- Installing and Configuring Maven for Build Automation and Dependency Management in Oracle Fusion Middleware Developing Applications Using Continuous Integration
Credits
Compile Java Mac
To navigate this Oracle by Example tutorial, note the following:
Compile Java File Mac
- Topic List:
- Click a topic to navigate to that section.
- Expand All Topics:
- Click the button to show or hide the details for the sections. By default, all topics are collapsed.
- Hide All Images:
- Click the button to show or hide the screenshots. By default, all images are displayed.
- Print:
- Click the button to print the content. The content that is currently displayed or hidden is printed.
Compiling Java On Mac
To navigate to a particular section in this tutorial, select the topic from the list.