Skip to content

Instantly share code, notes, and snippets.

@saltnlight5
Created November 16, 2012 12:02
Show Gist options
  • Save saltnlight5/4086758 to your computer and use it in GitHub Desktop.
Save saltnlight5/4086758 to your computer and use it in GitHub Desktop.
java.sh
#!/usr/bin/env bash
# Author: Zemian Deng, Date: 2012-11-16_06:30:00
# java.sh - A Java wrapper that auto setup classpath to run in Cygwin, Unix or Linux shell.
# We assume this script is located in a subdiretory inside the application home directory.
# Example:
# app/bin/java.sh
# app/config/log4j.properties
# app/lib/log4j.jar
# Usage:
# bash> java.sh app.Hello
# bash> DEBUG=1 java.sh app.Hello
# bash> DEBUG=1 CP=build/classes/main java.sh app.Hello
# bash> DEBUG=1 CLASSPATH=build/classes/test CP=build/classes/main java.sh app.Hello
# Setup App Home Dir
APP_HOME=$(cd "$(dirname $0)/.." && pwd)
# Setup classpath - Default to user defined CP (will be append as suffix)
CP=${CP:=}
# Setup classpath - Pick up Maven development classpath if found
if [[ -d $APP_HOME/target/test-classes ]]; then CP="$APP_HOME/target/test-classes:$CP"; fi
if [[ -d $APP_HOME/target/classes ]]; then CP="$APP_HOME/target/classes:$CP"; fi
if [[ -d $APP_HOME/target/dependency ]]; then CP="$APP_HOME/target/dependency/*:$CP"; fi
# Setup classpath - Pick up release folders if found
if [[ -d $APP_HOME/config ]]; then CP="$APP_HOME/config:$CP"; fi
if [[ -d $APP_HOME/lib ]]; then CP="$APP_HOME/lib/*:$CP"; fi
# Setup classpath - Prefix existing CLASSPATH if found
if [[ -n "$CLASSPATH" ]]; then CP="$CLASSPATH:$CP"; fi
# Setup classpath - If CP is empty then default to current dir.
if [[ -z "$CP" ]]; then CP="."; fi
# Setup classpath - Convert to Windows path if we are in Windows OS
if [[ "$OS" == Windows* ]]; then CP=$(cygpath -mp "$CP"); fi
# Run Java command
JAVA=java
if [[ -n "$JAVA_HOME" ]]; then JAVA="$JAVA_HOME/bin/java"; fi
if [[ -n "$DEBUG" ]]; then echo "$JAVA -cp $CP $@"; fi
$JAVA -cp $CP $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment