【问题标题】:How to add java library reference to a shell script如何将 java 库引用添加到 shell 脚本
【发布时间】:2017-10-23 10:28:18
【问题描述】:

我在 springClasspath 文件中有以下 spring 参考 jars

    CLASSPATH=/apps/cab/spring/spring-core-3.1.1.RELEASE.jar:/apps/cab/spring/commons-logging-1.2.jar:/apps/cab/spring/spring-aop-4.2.2.RELEASE.jar:/apps/cab/spring/spring-beans-4.2.2.RELEASE.jar 
export CLASSPATH

我想将 springClasspath 文件引用到我的 shell 脚本文件 start_order.sh 以便我的 java 程序引用所需的 spring 依赖项。我该怎么做?

【问题讨论】:

  • springClasspath 是单独的文件吗?并且您想在其他脚本中使用其中定义的变量CLASSPATHCLASSPATH是什么类型的文件,除了变量初始化还有其他行吗?
  • springClasspath 是一个单独的文件。是的,我也想在其他脚本中使用 CLASSPATH 变量。springClasspath 文件只包含库依赖项

标签: java bash shell unix


【解决方案1】:

由于您计划在 shell 脚本中使用的 springClasspath 文件中有多个声明。您需要将它用于source 脚本中的文件以进一步使用变量。

通过在脚本中获取文件,您可以使其中定义的变量在运行脚本的子 shell 中可用。例如,在将解释器设置为bash 后的start_order.sh 行中,源脚本如下

#!/usr/bin/env bash

# Give the full path to the file if it is present in another location
. ./springClasspath 

# with the above source done, the variables can be used as below in your
# script
echo "$CLASSPATH"

记住export-ing 文件在这种情况下将不起作用,除非您的文件源和 还有脚本,以便它们在同一个父级中运行设置了变量的 shell。

【讨论】:

    猜你喜欢
    • 2011-10-31
    • 2020-08-13
    • 2016-12-08
    • 1970-01-01
    • 2012-02-18
    • 2012-02-05
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多