Hello I have a java program. I have a script that should execute the program. The script looks like this
sh compress.sh -f myfile.txt [-o myfile.hzip -s]
How do i create executable from my java program so that it can execute from this script?
Hello I have a java program. I have a script that should execute the program. The script looks like this
sh compress.sh -f myfile.txt [-o myfile.hzip -s]
How do i create executable from my java program so that it can execute from this script?
MyApp.java:
public class MyApp {
public static void main(String []args) {
//Do something with args...
}
}
compress.sh:
#!/bin/sh
#Specify your Java class here (must contain a main method)
JAVA_CMD="java MyApp"
#JAR variant (a more common approach)
#JAVA_CMD="java -jar myjar.jar"
#Map script arguments to java app arguments. Use flag specific variables, if it's more convinient for you.
while getopts "f:o:s" opt; do
case $opt in
f)
JAVA_CMD="$JAVA_CMD -f $OPTARG"
;;
o)
JAVA_CMD="$JAVA_CMD -o $OPTARG"
;;
s)
JAVA_CMD="$JAVA_CMD -s"
;;
*)
echo "Invalid usage..." >&2
exit 1;
;;
esac
done
#Additional validation (required options etc.)
#...
echo "Launching Java: $JAVA_CMD"
$JAVA_CMD
Compile your class, then run the script. You may be required to include an additional classpath argument in JAVA_CMD, if you're using any external libraries.
If you're going to use a JAR, then make sure, that your MANIFEST.mf has all the required information (especially a valid Main-Class entry). Further instructions can be found here.
If this is a Maven project, than you can create an executable JAR in one easy step using Maven Assembly Plugin. Just run mvn assembly:assembly.