How to launch a java CLI program, developed in Eclipse, from Cygwin bash shell?
I wanted to share here a useful script of mine. I am developing some java CLI software under Eclipse. For time to time, I find it useful to run it from the command line with custom arguments (with Cygwin in my case), instead of creating a new "Run configuration" i开发者_如何学JAVAn eclipse.
My eclipse project depends on some other "core" projects and a bunch of libs.
So I needed a bash launcher ...
So here is my solution :
I used to hardcode this whole CLASSPATH in a launcher script, but it was a pain to maintain.
So recently, I wrote a bash script that automatically parses the ".classpath" files recursively and generates CLASSPATH dynamically. This way, my launcher is always up to date. I have also added a "debug" option, that launches Java in remote debug mode.
Hope this helps someone.
#! /usr/bin/bash
# Eclipse CLI program launcher.
# ----------------------------------------------------------
# Constants
# ----------------------------------------------------------
# Main class
CLASS=your.main.class.Here
# ----------------------------------------------------------
# Parse arguments
# ----------------------------------------------------------
# Debugger mode ?
if [ "$1" = "debug" ]
then
shift
DEBUG_OPTIONS="-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,address=3409,suspend=y"
fi
# -------------------------------------------------------
# Setup the classpath from eclipse .classpath files
# -------------------------------------------------------
# Init classpath
CLASSPATH=""
# Process a single .classpath file
# This is a recursive function
# Arguments:
# $1 : Dir path where to search for a ".classpath" file
function build_classpath() {
# Aliases to arguments
local folder="$1"
local file="$folder/.classpath"
# ".classpath" file does not exists ? => exit
if [ ! -f "$file" ]
then
return
fi
# Parse the file with sed
# return a list of <type>:<path> pairs
local lines=`sed -ne 's/^.*kind="\(.*\)"\s\s*path="\(.*\)".*$/\1:\2/gp' $file`
# Loop on lines
for line in $lines
do
# Split the type and path values
local type=${line%:*}
local path=${line#*:}
# Switch on type
case $type in
"lib" )
CLASSPATH=$CLASSPATH:$folder/$path
;;
"output" )
CLASSPATH=$CLASSPATH:$folder/$path
;;
"src" )
# Recursive call for other projects, relative to the workspace (the parent dir)
build_classpath "..$path"
;;
esac
done
}
# Start building the classpath from the current project dir
build_classpath .
# Call java
java $DEBUG_OPTIONS -Xmx1024m -cp `cygpath -wp $CLASSPATH` $CLASS $@
精彩评论