-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();
    }
}
3

3 Answers 3

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;
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

Use this command:

jar cf jar-file input-file(s)

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.