简体   繁体   English

引发Java错误时停止AppleScript运行.jar

[英]Stop AppleScript Running .jar When Java Error Raised

I have an AppleScript which I am using to run a .jar file. 我有一个AppleScript,用于运行.jar文件。 The .jar file takes several inputs which were originally entered via the command line but now I enter into a .csv and get read into the .jar automatically. .jar文件需要几个输入,这些输入最初是通过命令行输入的,但是现在我输入.csv并自动读取.jar。 For unknown reasons, sometimes a number in the CSV is not read correctly leading to a NumberFormatException in the Java code. 由于未知原因,有时无法正确读取CSV中的数字,从而导致Java代码中出现NumberFormatException However, instead of breaking, my script continually tries to enter the invalid input in an infinite loop. 但是,我的脚本没有中断,而是连续尝试在无限循环中输入无效的输入。 Is there a way to amend my code so that when an error is raised by the .jar, the script stops? 有没有一种方法可以修改我的代码,以便当.jar引发错误时,脚本停止运行?

Here is my current code: 这是我当前的代码:

on RunFile(jar_location)
    do shell script "cd " & jar_location & " ; cat 'prompt.csv' | sh 'runScript.sh' 'WSO'"
end RunFile

After going over this in the comments, it's clear that the problem is that the .jar file is trying to be interactive — asking for some input at the cursor — and AppleScript's do shell script is not designed for that. 在注释中仔细研究了这一点之后,很明显的问题是.jar文件正在尝试进行交互-要求在光标处输入一些信息-而AppleScript的do shell script并非为此设计的。 AppleScript can get errors and outputs form the shell, but it cannot feed a response back to the shell, or tell if a shell script is waiting for input. AppleScript可以从shell获取错误和输出,但是它无法将响应反馈给shell,也不能告诉shell脚本是否正在等待输入。

if the .jar file cannot be operated in a non-interactive mode, then the only for AppleScript to make sure the process ends is to grab its process id number, wait a reasonable amount of time, and then send it a kill signal. 如果.jar文件不能在非交互模式下运行,则AppleScript要确保进程结束,唯一的办法是获取其进程ID号,等待一段合理的时间,然后向其发送终止信号。 That script would look like this: 该脚本如下所示:

on RunFile(jar_location)
    set pid to do shell script "cd " & jar_location & " ; cat 'prompt.csv' | sh 'runScript.sh' 'WSO' &> /dev/null & echo $!"
    -- wait 5 seconds, or whatever seems appropriate for the task to complete
    delay 5
    try
        do shell script "kill " & pid
    end try
end RunFile

The appended &> /dev/null & echo $! 附加的&> /dev/null & echo $! phrase detaches the shell script, allowing the AppleScript to move forward, and returns the process id of the process for later use. 短语分离外壳程序脚本,允许AppleScript向前移动,并返回该进程的进程ID供以后使用。 I've put the kill signal in a try block so that the script does not throw an error if the process has already exited normally. 我将kill信号放在try块中,以便如果进程已经正常退出,脚本不会引发错误。

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

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