How do I make my program, written in Java, usable to other people?

CaldwellYSR picture CaldwellYSR · Sep 23, 2011 · Viewed 9.1k times · Source

So I've written a funny little program and I want to show it to some of my friends. My friends, not being programmers, have no idea what to do if I send them the folder containing the necessary classes and files. I want to be able to email them something (or put it on a cd/thumbdrive) that they can then double click and have it run the program. I have absolutely no clue how to make this happen. I'm taking a class and we use linux computers (I use a mac when I'm not in class) and we have to javac the .java files and then java "File name" to make it run. I have friends on Mac's and PC's and I want them to be able to just click the program and have it go....

If it makes a difference the program is written using the object draw library.

Answer

Edwin Buck picture Edwin Buck · Sep 23, 2011

Use the jar command to build an executable jar file. It's basically a ZIP file with certain guarantees.

Within the jar file you will need (it's a reqirement) a /META-INF directory with a file in it called the manifest (/META-INF/MANIFEST.MF). It describes the "manifest" of the jar file, which is in some ways modeled off a shipping manifest.

Inside the MANIFEST.MF file, you need the directive

Main-Class: org.mystuff.path.Main

Which should direct the JVM to run the org.mystuff.path.Main class when the jar file is "exectued" via the command

java -jar myproject.jar

Note that JAR files tend to handle classpaths differently, in the sense that they ignore the CLASSPATH environmental variable, and the -classpath command line options. You need to add a classpath directive within the MANIFEST.MF file if you need to reference other JAR files. Use the manifest link above to see the details concerning embedding a classpath.

Depending on the simplicity of the "project", a single JAR file might be enough to ship to them; however, if you need more than that, you might find yourself having to ship a few files in a few directories. In that case, put the files (including the JAR file) and directories in a second zip file. While you could select from a number of different ways to "package" the items, I recommend

(layout inside the zip file)
/Readme.txt (a text file describing what to do for new comers)
/License.txt (a text file describing how liberal / restrictive you wish to be concerning your authorship rights.
(the license sounds like overkill, but without it nobody can prove they're not breaking the law)
/bin/myprogram.sh (shell script containing "java -jar ../lib/myprogram.jar")
/bin/myprogram.cmd (Windows batch file containing "java -jar ..\lib\myprogram.jar")
/lib/myprogram.jar (the executable jar file containing your compiled code)
/lib/otherjar.jar (other jar files, as necessary)

With such a zip-file structure, the installation instructions then become "unzip the zip file; change directory to "bin" and run "myprogram.whatever".