JAR FILE

ABOUT JAR FILES

JAR, which stands for Java Archive, is a package format that groups different Java files into a single file.  The Runnable JAR file can be double clicked on to run the Java Application.

CONSOLE APPLICATIONS

If your Java program is a console application, then running the JAR file by double clicking it will not launch the program.  Double clicking will run the program with javaw.exe instead of java.exe (and console applications need to run with java.exe).

To run a console application inside a JAR file, you should go to the terminal and type:

java -jar filename.jar

BATCH FILE

If you have a console application in a JAR file and you want to be able to double click on a file to run it, you could create a BATCH file.

Place the two following lines in the BATCH file (where filename is replaced by your JAR’s filename):


java -jar filename.jar
pause


Name the batch file, filename.bat and make sure it is in the same folder as your JAR file.  So you will have two files with the same name, but different extensions.

Now, double clicking on the batch file will run the FunJar.jar file using java.exe which will open a terminal window and show console output in it.

HOW TO CREATE A RUNNABLE JAR FILE IN ECLIPSE

1-First, make sure you’ve run your program at least once in Eclipse.

2-Right click on your project in the Package Explorer and select Export.

3-Under Java, choose Runnable Jar File and click Next.

4-Choose the correct Launch configuration (the class that contains your main method).

5-Set the Export destination by clicking Browse and naming your file (e.g., MyApp.jar).

6-Under Library handling, select "Extract required libraries into generated JAR" to include dependencies.

7-Click Finish.

8-Done.  Now go to the location and try running your JAR file.  Remember that if you program is a console application, you will need to either run it in the terminal or create a batch file.

FURTHER TESTING

We still need to check how an external text file works here.  Does it get placed inside the JAR file?  Does it need to be placed in the same folder as the JAR file?  Or perhaps using a specific path relative to the JAR file?