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

发布于 2024-09-08 11:18:35 字数 193 浏览 3 评论 0原文

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

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

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

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" in eclipse.

My eclipse project depends on some other "core" projects and a bunch of libs.

So I needed a bash launcher ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橙幽之幻 2024-09-15 11:18:35

所以这是我的解决方案:

我曾经在启动器脚本中对整个 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 $@

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 $@
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文