简体   繁体   English

从Shell脚本调用Java应用

[英]invoke a java app from shell script

I'm trying to invoke a java jar from a shell script. 我正在尝试从shell脚本调用Java jar。 It works with a windown batch script. 它与windown批处理脚本一起使用。 I tried to translate this working batch script into a shell script but don't get it working. 我试图将此可运行的批处理脚本转换为Shell脚本,但没有使其正常工作。 Where is the bug? 错误在哪里?

The original batch file (works perfectly): 原始批处理文件(完美运行):

@echo off
rem set the right host here
set host=example.com
set port=8080
set urlPartBeforeApiVersion=/project/api
rem geographical region
set west=47.358352
set south=8.493598
set east=47.406704
set north=8.560889
set numberOfVehicles=10
set idPerfix=SIM_KSDN128D
set vehicleSpeed=40
set gpsSendInterval=10
set cloudmadeApiKey=kldhfjsghjf83hf83hf83hf89whs89
java -jar %~p0dist/application.jar %host% %port% %west% %south% %east% %north% %numberOfVehicles% %idPerfix% %vehicleSpeed% %gpsSendInterval% %urlPartBeforeApiVersion% %cloudmadeApiKey%
pause

The shell script (doesn't work): Shell脚本(无效):

#!/bin/sh
host="example.com"
port="8080"
urlPartBeforeApiVersion="/project/api"
west="47.358352"
south="8.493598"
east="47.406704"
north="8.560889"
numberOfVehicles="10"
idPerfix="SIM_KSDN128D"
vehicleSpeed="40"
gpsSendInterval="10"
cloudmadeApiKey="kldhfjsghjf83hf83hf83hf89whs89"
java -jar $(dirname $0)/dist/application.jar $host $port $west $south $east $north $numberOfVehicles $idPerfix $vehicleSpeed $gpsSendInterval $urlPartBeforeApiVersion $cloudmadeApiKey

The application throws a Java NumberFormatException and exits. 该应用程序将引发Java NumberFormatException并退出。 I tested the script on cygwin on Windows 7 and on centos. 我在Windows 7和centos上的cygwin上测试了该脚本。

This is one of the many wonderful things that can happen when your bash script has DOS line separators. 这是当bash脚本具有DOS行分隔符时可能发生的许多奇妙的事情之一。

Run your script through dos2unix , sed -i 's/\\r//' , tr -d '\\r' or whatever you have available, and try again. 通过dos2unixsed -i 's/\\r//'tr -d '\\r'或任何可用的脚本运行脚本,然后重试。

For future reference, whenever you see that a line starts with garbage and is surreptitiously truncated, such as 供以后参考,每当您看到一行以垃圾开头并且被秘密截断时,例如

"xception [...]: For input string: "8080

rather than 而不是

Exception [...]: For input string: "8080"

You know that you're dealing with carriage returns. 您知道您正在处理回车。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM