Page 1 of 1

How to Create a Java Executable Jar File Rate Topic: ***** 3 Votes

#1 CarDriver  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Posted 12 August 2011 - 02:24 PM

*
POPULAR

This is a tutorial that should help you learn how to make a jar file quickly and easily for all of your programs. Creating a jar file is not difficult to do, and it is something that should certainly be learned by beginning Java programmers. If you prefer to learn by watching, check out my video tutorial:



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.

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.

This post has been edited by AVReidy: 09 November 2014 - 10:54 PM


Is This A Good Question/Topic? 16
  • +

Replies To: How to Create a Java Executable Jar File

#2 sahildave1991  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 45
  • Joined: 18-September 10

Posted 21 October 2011 - 10:58 PM

When i make my jar file from this procedure an error occurs:
Could not find the main class:frame. Program will exit


i have a frame class which one by one links to other classes (in all 18 different classes).
i had made this project in eclipse and this program was under New2 package.
i copied all the images, the src files and the class files.
Was This Post Helpful? 0
  • +
  • -

#3 CarDriver  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Posted 22 October 2011 - 06:54 AM

Funny enough, I'm having this problem for the first time as well, and I'm actually not sure how to fix it. I just came back to my tutorial to see if I was doing something wrong...
Was This Post Helpful? 1
  • +
  • -

#4 AMZDeCoder  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 102
  • Joined: 04-November 11

Posted 04 November 2011 - 12:15 PM

Creating a executable JAR file is fine, eclipse has inbuilt feature which does that for us, does any one know how to create an installer for a Java application.
Was This Post Helpful? 0
  • +
  • -

#5 CarDriver  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Posted 05 November 2011 - 09:05 AM

An installer is an application in itself, so you won't be simply writing a few command line commands to make one. If you don't want to create one from scratch, I'm sure there are some pre-made, open-source installers you could use. If you want to get into C#, Microsoft Visual Studio lets you 'publish' your application, which creates a generic installer for your application.
Was This Post Helpful? 0
  • +
  • -

#6 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Posted 05 November 2011 - 09:18 AM

This tutorial was so helpful and the YouTube video helped answer some questions I was wondering about. This tutorial was so helpful. I'll + rep this when I get onto my computer because I can't + rep on an iPhone. <_<

By the way what does "cfm" mean? It's on the command used to create the JAR.
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Posted 05 November 2011 - 09:22 AM

c --means create a jarfile with name
f --specifies the jarfile for something to be done with
m --Includes name: value attribute pairs from the specified manifest file manifest in the file at META-INF/MANIFEST.MF. A name: value pair is added unless one already exists with the same name, in which case its value is updated.

On the command line, the letters m and f must appear in the same order that manifest and jarfile appear. Example use:

jar cmf myManifestFile myFile.jar *.class

You can add special-purpose name: value attribute pairs to the manifest that aren't contained in the default manifest. Examples of such attributes would be those for vendor information, version information, package sealing, and to make JAR-bundled applications executable.

Reference
Was This Post Helpful? 3
  • +
  • -

#8 delhinagar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-April 12

Posted 24 April 2012 - 04:17 AM

hello, can you tell me how to bundle images into jar? i tried your way and it worked perfectly. I tried something from myself too. I added images with class files like this box.class clock.png search.png . I added all images like this but its not showing images. Is there anyway?i dnt use beans, i just use notepad or textpad sometimes.Thanks
Was This Post Helpful? 0
  • +
  • -

#9 shadowhunterO  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 12
  • Joined: 24-June 12

Posted 25 June 2012 - 10:11 AM

For linux you can just rename a document to .jar, I do't know if it works for other operating systems though.
Was This Post Helpful? 0
  • +
  • -

#10 sarathy.290  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-July 12

Posted 27 July 2012 - 03:09 AM

i created a jar file using this tutorial.. thanks dude...
Was This Post Helpful? 0
  • +
  • -

#11 CarDriver  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Posted 12 August 2012 - 06:59 PM

View PostshadowhunterO, on 25 June 2012 - 12:11 PM, said:

For linux you can just rename a document to .jar, I do't know if it works for other operating systems though.
You can rename things in just about every operating system. But you can't properly run an executable if it's not executable - you need a compiler (or the jar command in this case).
Was This Post Helpful? 0
  • +
  • -

#12 nanditha16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-September 13

Posted 13 September 2013 - 12:57 PM

Hi,

I tried to create a jar file following your post. I was able to create the executable file. The code perfectly works fine on Eclipse IDE. but when I create the executable jar file and try to run that, or even if I try to run the code from cmd I get the below said error. Can you please help me why I'm getting this error please.

U:\7 to 13_sept\13_9\S4B_v4>java -jar S4B_v4.jar
Exception in thread "main" java.lang.UnsupportedClassVersionerror: whi/s4b/gui/m
ain/S4B_GUI : Unsupported major.minor version 51.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(Unknown Source)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$000(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: whi.s4b.gui.main.S4B_GUI. Program will exit.


Was This Post Helpful? 0
  • +
  • -

#13 arnavkumartechno  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-October 13

Posted 30 October 2013 - 04:40 AM

Re: How to Create a Java Executable Jar File

It is good to know how to create Java Executable Jar file. But i would prefer to do it with the eclipse or jDeveloper
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1