-1

I have finished writing a project in the IntelliJ IDEA using Java. I can not figure out how to get the project from a saved file in the IDEA to an executable on my computer (I am on Windows 10 if that matters). Then, I want to be able to put the executable on my Girlfriend's computer (mac) because it's a gift for our anniversary. She's not very tech-savvy (apparently neither am I...) so I want to be able to just drag and drop something onto her desktop and have her be able to double click it and have it work without having to install anything to her computer. (I was looking at Jar files, but something I read said that mac's do not run Jar files unless I install some things).

So what I'm really asking is:

1)Using IntelliJ, how do I finalize my project (make a JAR file)? I have been unable to find directions for this that have actually worked.

2)What file do I need to make a batch file call to get it to run my program once it is a JAR? Does it just call the JAR as a whole, or a specific .class? (Someone suggested a bash file, but I have not heard of this before, so I am open to other suggestions besides batch files. I am currently looking into what bash files are)

3)Can I make the batch file call the file indicated in question 2 via relative path, thus allowing me to keep it in the JAR or in a folder with the JAR and making a shortcut to it on a desktop (for example)?

Daniel
  • 3
  • 3
  • Questions, by convention, end in a question mark. Since there isn't one in your contribution, I'm wondering, if you have a question. – IInspectable Oct 18 '16 at 01:26
  • Possible duplicate of [Make Java program as independent exe (run without JVM)](http://stackoverflow.com/questions/6700311/make-java-program-as-independent-exe-run-without-jvm) – MordechayS Oct 18 '16 at 02:08
  • You're talking about building your project so the final product is a single executable file? Java projects tend to build as JAR files and then you execute them via command line/terminal. You could write a bash script that runs your JAR file, but it would mean you're copying 2 files to the Mac rather that 1 (both the JAR and then bash script). – Harmelodic Oct 19 '16 at 14:45
  • You'll have to make sure that Java is installed on the Mac though and if you want to test the bash script works, you'll need to test on the Mac or test on a Virtual Unix machine. – Harmelodic Oct 19 '16 at 14:47

1 Answers1

0

Building a JAR in IntelliJ

Option 1 - Long way round, getting IntelliJ to build your JAR

  1. Hit Ctrl+Shift+Alt+S to open your Project Structure settings menu.
  2. Go to Artifacts
  3. Click the green +
  4. Select JAR > Empty.
  5. Name your JAR, for example Gift
  6. Right click on all the files in the Available Elements that you want to put into your JAR (often everything) and select Put into Output Root
  7. Click Apply. Click OK.
  8. Go to Build > Build Artifacts... > Build.
  9. You'll find your JAR in out/artifacts/{JAR_NAME}/{JAR_NAME}.jar

Option 2 - Short way (Maven only)

If you're using a dependency manager like Maven and you've adhered to the Maven project structure, just use the command:

> mvn clean package

and you'll find your JAR in your target/ directory.

In either option, you may have to define your .class file that contains your main() method.

Running a JAR

The normal way to run a JAR file would be to go into cmd (Windows) or a terminal (Mac & Linux) and use the command:

> java -jar {JAR_NAME}.jar {args}

where {JAR_NAME} is the file name of your JAR and {args} are any arguments that need to be defined up front.

If you want to not use a command line you could write a script that runs the JAR for you. Then you can double-click/run the script and it runs the JAR for you.

For Windows

I'd recommend writing a simple .bat or "batch" script that will run the file for you.
Keep the batch script in the same directory as your JAR file and just have it contain:

java -jar ./{JAR_NAME}.jar {args}

You could use a Powershell script or something similar though.

For Mac/Linux

As Mac (and Linux) are Unix systems we can use a .sh or "bash" script to run the JAR. Again keep the bash script in the same directory as your JAR file and use:

#!/bin/bash

java -jar ./{JAR_NAME}.jar {args}

.bat script is exclusive to Windows systems, .sh scripts are exclusive to Unix systems.
Because Windows and Unix are two very different systems, you're not going to find an executable file that works for all systems.

Converting your JAR into an executable

You could convert your JAR into a standard Mac "app" application bundle by following some instructions here.
However, I wouldn't recommend this as it isn't really necessary when you have a simple script/command option and for this method, you'd most likely need access to a MacOS machine with development tools.

Community
  • 1
  • 1
Harmelodic
  • 5,682
  • 1
  • 28
  • 31