First of all, I'm going to assume you have written a working program and have at least one Java file for it. This tutorial will cover compiling the Java file into a class file, but that is not the main focus here.
1. Right-click and create a new folder on your desktop. The title doesn't matter. This is where you will store all of the files that will make up your jar file. Using a folder is helpful when you have a project of multiple classes.
2. Either save or drag and drop your Java files into your folder.
3. Now you will need to open the command prompt. Do this by holding the Windows key, then pressing the R key. This should bring up your Run application in which you should type "cmd" - press OK and this should bring up your command prompt.
4. In the command prompt, type "cd Desktop" to change your directory to the desktop, the location of your folder. Type "dir" to show what is in the Desktop directory. You should see the folder that has your Java files in it among the list. Type "cd <foldername>" or, for example, "cd MyJavaFolder" - this should change your directory to your folder. To see what is in it, type "dir" again.
5. By typing "dir" you should see your Java files inside of the folder. In order to turn these into class files, we need to compile them by using the "javac" command. This command requires the JDK and it requires you to change the classpath. This will not be covered in this tutorial. To compile the files, type "javac MyJavaFile.java" - to compile multiple files just put a space between them and use the same command "javac MyJavaFile1.java MyJavaFile2.java".
6. Type "dir" again and you should see your Java files and now your class files as well. You can minimize the command prompt and open the folder on your desktop. Inside the folder, right-click and create a new text document (notepad). In notepad, you need to show what your main class is for the jar file. Here's how you do this:
Main-Class: MyMainClass |
This part is very important, so make sure to take note of the capitalization and the spacing. Don't add the .class ending to your specified main class. You MUST press enter twice from the line you write on so that your curser ends up exactly where I placed the vertical bar. From that point, save it (to your folder of course) as "MyManifest.txt". The name doesn't really matter, but it's called a manifest document.
7. Now go back into your command prompt and navigate back to your folder if you didn't just minimize the command prompt. Type "dir" once in your folder directory and you should see your manifest document.
Before I give you the secret to create a jar file, check out tunejet.net.
8. Here's how you make your jar file once you have the files all ready to go. You should be in your folder directory in the command prompt. Type "jar cfm JarFileName.jar MyManifest.txt MyJavaFile1.class MyJavaFile2.class" - Type "dir" and you should see your jar file! You choose what your jar file is called in this command as well. The order matters.
9. Now, what good does a jar file do if you can't run it? Well, not much. If your program is made to run in the command line, then you will still have to run it by typing a command (unfortunately). Otherwise, if you have a GUI, you can go to your folder and double-click your jar file. To run a jar file from the command prompt, type "java -jar MyJarFile.jar".
Now you have a way to show off your programs on someone else's computer without having to download the JDK and configure the classpath and so on. All you need is the JRE, which most people have. If you're not into making GUIs yet, your jar file is still just as portable as the ones you can double-click. All you need to run the jar file is a computer with the JRE and, of course, the command to run a jar file.
Making executable jar files is a fun way to wrap up your code into a portable package that can run almost anywhere. I hope this tutorial helped you! Let me know if you have further questions, and let me know if I made a mistake. But again, I hope it made sense and wasn't too complicated or difficult to copy. If you need some more help, try my YouTube tutorial on this.
This post has been edited by AVReidy: 06 March 2013 - 05:26 PM