Back

Packaging and sharing your projects

An effective way of sharing your applications with your collaborators is preparing self-running jar files. How to prepare such files using psychWithJava is outlined below using the HelloPsychophysicist example. A more complete discussion can be found at http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html and at http://java.sun.com/docs/books/tutorial/deployment/jar/index.html. (Note: to run the example jar files you don't need to install the psychWithJava package.)

Creating JAR files with command line tools

  • Open a terminal and navigate to the directory of your project. Extract the class file(s) needed from the PsychWithJava package. For example, to extract the FullScreen.class
    jar xvf <full_path_to_psychWithJava> psychWithJava/
    where <full_path_to_psychWithJava> is the full path to the psychWithJava package as explained here (For example /home/boyaci/javaClasses/psychWithJava.jar on Linux, or /Users/boyaci/javaClasses/psychWithJava.jar on Mac OS X.) This will create psychWithJava/FullScreen.class file.
  • Create a manifest file in the project directory. This file, in its simplest form, contains only two lines
    Manifest-Version: 1.0
    Main-Class: HelloPsychophysicist
    
    Main-Class indicates the name of the class that has the main method in your project. If you haven't yet built your project, see Building and running java projects here.
  • Pack your application. Open a terminal (or MS-DOS prompt) and navigate to the directory of your project, and type
    jar -cvfm HelloPsychophysicist.jar manifest.mf HelloPsychophysicist.class psychWithJava/*.class *.png
    and press enter. The first argument is the name of the resulting .jar file, the second argument is the manifest file, followed by the names of the necessary .class files. Required auxilary media files (for example image files) should be included, as well.
  • Creating JAR files with Eclipse

  • Start Eclipse and right click on the default package under HelloPsychophysicist project on the package explorer pane, then click on Import


    This image is altered to fit the page. Click on the image to see it in its actual size
  • In the Import window, choose Archive File under General and click Next


    This image is altered to fit the page. Click on the image to see it in its actual size
  • In the next window choose the archive file (jar file). Choose the .java file(s) you want to import (for example FullScreen.java). Finally choose the project folder to import into, and click Finish.


    This image is altered to fit the page. Click on the image to see it in its actual size
  • Next right click on the default package under HelloPsychophysicist project on the package explorer pane, then click on Export


    This image is altered to fit the page. Click on the image to see it in its actual size
  • On the Export window choose JAR file and click Next


    This image is altered to fit the page. Click on the image to see it in its actual size
  • JAR Export window should appear. Make sure to check the boxes next to the auxilary image files psychophysik.png and fechner.png


    This image is altered to fit the page. Click on the image to see it in its actual size
  • Navigate to the default package under Hello Psychophysicist and make sure that HelloPsychopysicist.java is exported as well. Similarly make sure that psychWithJava/FullScreen.java is also exported.


    This image is altered to fit the page. Click on the image to see it in its actual size


    This image is altered to fit the page. Click on the image to see it in its actual size
  • Select a destination name for the exported file (it isn't visible in the screenshot below but the name was HelloPsychophysicist.jar) and press Next


    This image is altered to fit the page. Click on the image to see it in its actual size
  • Click Next again


    This image is altered to fit the page. Click on the image to see it in its actual size
  • In the final window, check the radio button next to "Generate the manifest file," and select your main class. Click Finish.


    This image is altered to fit the page. Click on the image to see it in its actual size
  • Running JAR files

    Once the self-running jar file is created, you can deploy it either by a mouse click or double on the file on your hard drive (this may work even if the file is on a web page), or manually from a terminal (MS-DOS prompt) by typing
    java -jar HelloPsychophysicist.jar
    Anyone who has Java installed on his/her computer can run this jar file without installing psychWithJava package, as well.