2010-07-06 34 views
0

我想在这里分享我的一个有用的脚本。 我正在Eclipse下开发一些java CLI软件。 不时在eclipse中创建一个新的“运行配置”,我觉得从命令行运行自定义参数(在我的例子中是Cygwin)是非常有用的。如何从Eclipse开发一个从Cygwin bash shell开发的java CLI程序?

我的eclipse项目依赖于一些其他的“核心”项目和一堆库。

所以,我需要一个bash启动...

回答

2

因此,这里是我的解决方案:

我用硬编码在启动程序脚本这整个CLASSPATH,但它是维持疼痛。

所以最近,我写了一个bash脚本,它自动分析“.classpath”文件并动态生成CLASSPATH。这样,我的发射器始终处于最新状态。我还添加了一个“调试”选项,以远程调试模式启动Java。

希望这可以帮助别人。

#! /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 [email protected] 
+1

你也可以看看maven - exec任务可以很容易地从命令行(cygwin/bash/dos/whatever)启动maven配置项目。 – serg10 2010-07-13 16:32:19