【问题标题】:invoke a java app from shell script从 shell 脚本调用 java 应用程序
【发布时间】:2013-07-29 16:28:51
【问题描述】:

我正在尝试从 shell 脚本调用 java jar。它适用于窗口批处理脚本。我试图将这个工作批处理脚本翻译成一个 shell 脚本,但没有让它工作。错误在哪里?

原始批处理文件(完美运行):

@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

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

应用程序抛出 Java NumberFormatException 并退出。我在 Windows 7 和 centos 上的 cygwin 上测试了脚本。

【问题讨论】:

  • 粘贴完整的堆栈跟踪,您传递了一些应用程序没有预料到的错误
  • 你遇到了什么错误?
  • “线程“main”中的异常 java.lang.NumberFormatException:对于输入字符串:“8080 at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) ) 在 java.lang.Integer.parseInt(Unknown Source) 在 application.Application.main(Application.java:36)
  • 您解析为int 的值是否由您在命令行中传递的参数之一表示?
  • 你能把#!/bin/sh 改成#/bin/bash 并重试吗?另外,尝试将 echo 暂时放在 java 之前,看看实际传递了什么?

标签: java bash shell jar


【解决方案1】:

这是当您的 bash 脚本具有 DOS 行分隔符时可能发生的许多奇妙事情之一。

通过dos2unixsed -i 's/\r//'tr -d '\r' 或任何可用的方式运行您的脚本,然后重试。

为了以后参考,每当你看到一行以垃圾开头并被偷偷截断,比如

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

而不是

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

您知道您正在处理回车。

【讨论】:

    猜你喜欢
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    相关资源
    最近更新 更多