Can run a jar file without knowing the jar file name

老子叫甜甜 提交于 2021-01-27 12:39:49

问题


Can any one tell how to run a jar file using java -jar command without knowing the Jar's full name, I know the prefix but the version of the jar file is dynamic,so I don't want to update my sh for every deplolyment

 #!/bin/bash

PROJECT_HOME=/opt/services/testing-batchp
PROJECT_JAR=batch-1.0.8-SNAPSHOT.jar
[ -f /etc/environment ] && . /etc/environment

nohup java -jar -Dspring.config.location=${PROJECT_HOME}/config/ ${PROJECT_HOME}/${PROJECT_JAR} $1 $2 >/dev/null 2>&1 &

I just modified the variable PROJECT_JAR="batch*.*-SNAPSHOT.jar" but its not working. Here the version of the jar will change in each deployment. Please help


回答1:


Try the below approach for executing script without mentioning the jar's release version.

 #!/bin/bash

PROJECT_HOME=/opt/services/testing-batchp
PROJECT_JAR=batch-*.*.jar
[ -f /etc/environment ] && . /etc/environment

nohup java -jar -Dspring.config.location=${PROJECT_HOME}/config/ ${PROJECT_HOME}/${PROJECT_JAR} $1 $2 >/dev/null 2>&1 &

Make sure that only one valid jar is there in the deployment folder.




回答2:


You can try setting the dynamic variable as below;

PROJECT_JAR=`ls -r batch*.*-SNAPSHOT.jar | head -1`

This must do the job. However, please note the ticks(`) enclosing the command to enable substitution.



来源:https://stackoverflow.com/questions/61361064/can-run-a-jar-file-without-knowing-the-jar-file-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!