Skip to content

Instantly share code, notes, and snippets.

@Theldus
Last active March 13, 2017 01:34
Show Gist options
  • Save Theldus/369d7e268ed55c465800d51f23d41449 to your computer and use it in GitHub Desktop.
Save Theldus/369d7e268ed55c465800d51f23d41449 to your computer and use it in GitHub Desktop.
#!/bin/bash
############################
####Make Jar by Theldus#####
############################
usage()
{
echo -e "Usage: \n$0 <main-class> <.class folder>"
echo " main-class : Your main class, without extension"
echo " .class folder: Where are your binary files"
}
checkargs()
{
if [ "$#" -ne 2 ]
then
usage
else
#Lets check if the class folder exists.
if [ ! -d "$2" ]
then
echo -e "\033[1;31mYour directory does not exist!, plz check the parameters\033[0m"
usage
else
javaVersion=$(javac -version 2>&1 > /dev/null | cut -d' ' -f2)
mainClass=$(echo "$1" | sed 's/\./\//g')
mainClass="$2/$mainClass.class"
file=$(basename $mainClass)
#Search for main
mainJavap=$(javap $mainClass 2> /dev/null | grep "public static void main")
if [ -z "$mainJavap" ]
then
echo -e "\033[1;31mThe file\033[0m \033[1;36m$file\033[0m \033[1;31m is really your main class?\033[0m"
else
echo -e "\033[1;32mThe file\033[0m \033[1;36m$file\033[0m \033[1;32mactually looks like the main file, ok :D\033[0m"
fi
mkdir -p "$2/META-INF"
echo "Manifest-Version: 1.0" > $2/META-INF/MANIFEST.MF
echo "Created-By: $javaVersion (MakeJar by Theldus)" >> $2/META-INF/MANIFEST.MF
echo "Main-Class: $1" >> $2/META-INF/MANIFEST.MF
echo -e "\033[1;32mBulding jar...\033[0m"
pushd . &> /dev/null
cd $2
zip -r $1.jar ./* &> /dev/null
popd &> /dev/null
rm -rf $2/META-INF
file=$(basename "$2/$1.jar")
echo -e "\033[1;33mFile\033[0m \033[1;36m$file\033[0m \033[1;33mcreated with success, ;)\033[0m"
fi
fi
}
checkargs "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment