Skip to content

Instantly share code, notes, and snippets.

@iamvickyav
Last active December 21, 2022 13:26
Show Gist options
  • Save iamvickyav/bbefd3f8cb97ea30b3bf93387f6baf4b to your computer and use it in GitHub Desktop.
Save iamvickyav/bbefd3f8cb97ea30b3bf93387f6baf4b to your computer and use it in GitHub Desktop.

Creating JAR file using CMD

If your source code has other jar files as dependency

Folder Structure

.
├── com
│   ├── vicky
│       └── Main.java

Main.java

package com.vicky;

import org.apache.commons.lang3.StringUtils;
import java.util.*;

public class Main {

	public static void main(String arg[]) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Please Enter a String");
		String enteredString = sc.next();

		String reversedString = StringUtils.reverse(enteredString);

		if (enteredString.equals(reversedString)) {
			System.out.println("Given String is Palindrome");
		} else {
			System.out.println("Given String is not a Palindrome");
		}
		sc.close();
	}
}

Above code has a dependency with Apache's Common Lang

Steps to create jar file

All commands mentioned below has to run from parent directory of the project

  1. Download apache commons lang jar file & keep it inside the parent directory
.
├── commons-lang3-3.12.0.jar
├── com
│   ├── vicky
│       └── Main.java
  1. Command to compile .java file to .class file

javac -classpath commons-lang3-3.12.0.jar com/vicky/*.java

  1. Create a Manifest.txt file with following content in parent directory

(Note: Its mandatory to add an empty line at the end)

Class-Path: commons-lang3-3.12.0.jar
Main-Class: com.vicky.Main
  1. Command to create Jar file
jar cfm Palindrome.jar Manifest.txt com/vicky/*.class
  1. Command to execute Jar file
java -jar Palindrome.jar 

If your source code can run on its own & doesn't have other jar files as dependency

Folder Structure

.
├── com
│   ├── vicky
│       └── Main.java

Main.java

package com.vicky;

import java.util.*;

public class Main {

	public static void main(String arg[]) {
		Scanner sc = new Scanner(System.in);
		
    		System.out.println("Please Enter a String");
		String enteredString = sc.next();
    
		StringBuilder input = new StringBuilder();
    		input.append(enteredString);
    
		String reversedString = input.reverse().toString();

		if (enteredString.equals(reversedString)) {
			System.out.println("Given String is Palindrome");
		} else {
			System.out.println("Given String is not a Palindrome");
		}
		sc.close();
	}
}

The above code uses only Java's in-built classes.

Steps to create jar file

All commands mentioned below has to run from parent directory of the project

  1. Command to compile .java file to class file

javac com/vicky/*.java

  1. Command to create Jar file
jar cfve Palindrome.jar com.vicky.Main com/vicky/*.class
  1. Command to execute Jar file
java -jar Palindrome.jar 

Doing things with Maven

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>
                                    com.vicky.Main
                                </mainClass>
                            </transformer>
                        </transformers>
                        <createDependencyReducedPom>
                            false
                        </createDependencyReducedPom>
                    </configuration>
                    </execution>
                </executions>
            </plugin>

Thats all folks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment