-4

How to create a jar file of Java source file?

I have one class called CreateJar which has function Calculate which calculate the addition of two integers. I called this function in another class called UseFunction, but I want to create a jar file of CreateJar class and want to import this in UseFunction class. I Googled for it but not get any solution.

Below are my classes:

CreateJar

package com.dir;

public class CreateJar {

    public int Calculate(int x,int y)
    {
        int z =x+y;
        return z;
    }
}

UseFunction

package com.dir;

public class UseFunction {
    CreateJar jar = new CreateJar();

    public UseFunction()
    {
        System.out.println(jar.Calculate(5, 6));
    }

    public static void main(String args[])
    {
        new UseFunction();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jivan
  • 91
  • 2
  • 3
  • 8

3 Answers3

4

1.Create a file named CreateJar.java

package com.dir;

public class CreateJar {
    public int Calculate(int x,int y)
    {
        int z =x+y;         
        return z;
    }    
}

2.Create a file named UseFunction.java:

package com.dir;

//import com.dir.CreateJar;

public class UseFunction {
    CreateJar jar = new CreateJar();
    public UseFunction()
    {
        System.out.println(jar.Calculate(5, 6));
    }

    public static void main(String args[])
    {
        new UseFunction();
    }

}

3.Compile the first file:

javac CreateJar.java

The compliled result will be "CreateJar.class"

4.Create directory structure for the package "com.dir".To do that,simply create a directory "com" and another directory "dir" in "com".

5.Copy or move "CreateJar.class" to com/dir

6.Pack it with Jar:

jar -cf myLib.jar com\dir\CreateJar.class

7.Compile "UseFunction.java" by referencing dir.jar:

javac -cp myLib.jar UseFunction.java
or
javac -classpath myLib.jar UseFunction.java

8.Note:If your UseFunction.java is not in the same package(com.dir) as the referenced class(CreateJar).Then you may need to add this line in UseFunction.java.

import com.dir.CreateJar;
SONIC3D
  • 489
  • 4
  • 6
1

The basic format of the command for creating a JAR file is:

jar cf jar-file input-file(s)

The options and arguments used in this command are:

  • The c option indicates that you want to create a JAR file.
  • The f option indicates that you want the output to go to a file rather than to stdout.
  • jar-file is the name that you want the resulting JAR file to have. You can use any filename for a JAR file. By convention, JAR filenames are given a .jar extension, though this is not required.
  • The input-file(s) argument is a space-separated list of one or more files that you want to include in your JAR file. The input-file(s) argument can contain the wildcard * symbol. If any of the "input-files" are directories, the contents of those directories are added to the JAR archive recursively.

The c and f options can appear in either order, but there must not be any space between them.

For more details visit here

Barranka
  • 20,547
  • 13
  • 65
  • 83
Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82
0

Use this command:

jar cf jar-file input-file(s)

Try this link : http://docs.oracle.com/javase/tutorial/deployment/jar/build.html

Vineet Singla
  • 1,609
  • 2
  • 20
  • 34