java - 如何从 Cygwin bash shell 启动在 Eclipse 中开发的 java CLI 程序?

标签 java eclipse cygwin classpath

我想在这里分享我的一个有用的脚本。 我正在 Eclipse 下开发一些 java CLI 软件。 有时,我发现使用自定义参数(在我的例子中使用 Cygwin)从命令行运行它很有用,而不是在 eclipse 中创建新的“运行配置”。

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

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

最佳答案

这是我的解决方案:

我曾经在启动器脚本中对整个 CLASSPATH 进行硬编码,但维护起来很痛苦。

所以最近,我写了一个bash脚本,它自动递归地解析“.classpath”文件并动态生成CLASSPATH。这样,我的启动器始终是最新的。我还添加了一个“调试”选项,可以在远程 Debug模式下启动 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 $@

关于java - 如何从 Cygwin bash shell 启动在 Eclipse 中开发的 java CLI 程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3184643/

相关文章:

java - 使用 java 和 XPath 搜索 xml 文档中的属性

java - 太多的 break 语句会破坏你的代码

java - 当我导出 Java 程序时,为什么出现框架而不出现面板?

amazon-web-services - 在 Windows/Cygwin 上配置 AWS SSH key

ubuntu - 使用 WSL Windows 10 中的 conda

linux - 有没有办法将密码添加到 Cygwin

java - 从 Arraylist<String> 获取重复值,然后在另一个 Arraylist 中获取这些项目

eclipse - 如何找到 ITextViewer 内容的项目?

android - 在其他方法中使用 setBackgroundResource?

java - 无法访问数组java中的对象