Sunday, 7 February 2016

Distribute your Java classes in a JAR file

The source of the information given in this post is http://www.skylit.com/javamethods/faqs/createjar.html
In my opinion, the simplest and quickest way to do this is using the command line.


Steps
  1. Start the Command Prompt. The easiest way is to Shift-Right Click on an empty spot in your Windows (File) Explorer. Make sure no files are selected, though. The idea is that your Windows Explorer should be in the location of your Java files.
  2. Navigate to the folder that holds your class files:
    Copy the path from Windows Explorer's address bar (more details about WE here), then type the following into the command prompt window:

    cd /d 
  3. minding the space at the end, then paste the path that you copied from Windows Explorer's address bar. Press Enter.
  4. If you haven't set your path to include JDK’s binaries (compiler and utilities, including the one that creates jar files), you need to do so by adding it to your default path (your folder may differ depending on the version of the JDK that you installed)
    path %path%;C:\Program Files\Java\jdk1.8.0_83\bin
  5. Compile your class(es):
    javac *.java
  6. Create a manifest file and your jar file (Replace IA with the relevant name to your project):
    echo Main-Class: IA > manifest.txt
    jar cvfm IA.jar manifest.txt *.class
    ---- or ----
    jar cvfe IA.jar IA *.class
  7. Test your jar:
    java -jar IA.jar
  8. Create a batch file so your end user can double click on it to run your command line application (you may also use Notepad to do this):
    copy con run.bat    (creates the file)
    java -jar IA.jar    (Press CONTROL Z and Enter to finish and save the file)
  9. Double click on run.bat to test it.

No comments:

Get back the command prompt in Windows 10

If you've updated your Windows 10 system to the Creators Update (or newer), you may have noticed that the contextual menu (shift+right c...