Java 每次获取shell脚本的退出值255
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6384565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting exit value of a shell script 255 every time
提问by shoaib
I have a jar say test.jar having TestJar as its main class, a shell script jar_executor.sh,and a java file. My test.jar will return 1 if we pass 1 as an argument and 2 if we pass any other value. My shell script is executing test.jar as follow
我有一个 jar 说 test.jar 有 TestJar 作为它的主类、一个 shell 脚本 jar_executor.sh 和一个 java 文件。如果我们传递 1 作为参数,我的 test.jar 将返回 1,如果我们传递任何其他值,则返回 2。我的 shell 脚本正在执行 test.jar 如下
#!/usr/bin/ksh
java TestJar 1 -cp test.jar
return_code=$?
exit $return_code
In java file I am creating a process and executing shell script and taking its exitvalue with following code
在java文件中,我正在创建一个进程并执行shell脚本并使用以下代码获取其退出值
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(sh jar_executor.sh);
int exitVal = process.waitFor();
System.out.println(exitVal);
This exitVal variable should print 1 or 2 as per argument we are passing but it is printing 255 everytime.
If I use echo $return_code
in shell script then I am getting correct value.
Please help me why I am getting value 255 with exit
. Thanks in advance!!!
这个 exitVal 变量应该根据我们传递的参数打印 1 或 2,但它每次都打印 255。如果我echo $return_code
在 shell 脚本中使用,那么我会得到正确的值。请帮助我为什么我用exit
. 提前致谢!!!
回答by Peter Lawrey
255 or -1 is an application defined exit code, you would have to read the code to know what it means.
255 或 -1 是应用程序定义的退出代码,您必须阅读代码才能知道它的含义。
A Java application which exits normally, returns 0 and if it throws an Exception, returns 1.
正常退出的 Java 应用程序返回 0,如果抛出异常,则返回 1。
回答by Richard Hansen
It looks like it might be a Java bug. Others have reported issues similar to what you've seen; see this Java bug report. The Java devs don't think there's a bug but I'm suspicious. The code JRE uses to get the return value from a spawned process is hairy, and I wouldn't be surprised if there's a race condition or other concurrency bug.
看起来它可能是一个Java错误。其他人报告了与您所看到的类似的问题;请参阅此Java 错误报告。Java 开发人员认为没有错误,但我很怀疑。JRE 用于从生成的进程中获取返回值的代码很复杂,如果存在竞争条件或其他并发错误,我不会感到惊讶。
I'm guessing that the JRE fails to capture the return code if the spawned process exits very quickly. If my suspicion is correct, adding sleep 1
to your shell script will cause it to work.
我猜如果生成的进程退出非常快,JRE 将无法捕获返回代码。如果我的怀疑是正确的,添加sleep 1
到您的 shell 脚本中将使其工作。
回答by Marco
You're probably wrong in invoking the script. Try:
您调用脚本可能是错误的。尝试:
Process process = runtime.exec("sh -c jar_executor.sh");
Note the "-c" flag that means you're calling the shell to execute the command.
请注意“-c”标志,这意味着您正在调用 shell 来执行命令。
回答by holgero
Your shell script does not call java correctly:
您的 shell 脚本没有正确调用 java:
java TestJar 1 -cp test.jar
You need to set the options for the java command before you mention your main class name. All arguments after your main class name are arguments for your main class and are no longer options for the java VM. So in your case there is no classpath specified for java. I get the following error message when I execute your script: Error: Could not find or load main class TestJar. To fix, you have just to re-order the arguments in your jar_executor.sh script:
在提及主类名之前,您需要设置 java 命令的选项。主类名之后的所有参数都是主类的参数,不再是 Java VM 的选项。因此,在您的情况下,没有为 java 指定类路径。执行脚本时收到以下错误消息:错误:无法找到或加载主类 TestJar。要修复,您只需重新排序 jar_executor.sh 脚本中的参数:
java -cp test.jar TestJar 1
I cannot reproduce the 255 on my PC, so I can only guess where that comes from: Either your java command returns an error code of 255 instead of 1 when it fails to load the main class or your korn shell (/usr/bin/ksh) sets this return value when the script is aborted.
我无法在我的 PC 上重现 255,所以我只能猜测它的来源:当您的 java 命令无法加载主类或您的 korn shell (/usr/bin/ ksh) 在脚本中止时设置此返回值。
Here are all sources I used:
以下是我使用的所有来源:
jar_executor.sh
jar_executor.sh
#!/bin/sh -e
java -cp test.jar TestJar 2
return_code=$?
exit $return_code
TestJar.java
测试文件
public class TestJar {
public static void main(final String[] args) {
System.exit(Integer.parseInt(args[0]));
}
}
JarRunner.java
JarRunner.java
import java.io.IOException;
public class JarRunner {
public static void main(final String[] args) throws IOException, InterruptedException {
final Runtime runtime = Runtime.getRuntime();
final Process process = runtime.exec("sh jar_executor.sh");
final int exitVal = process.waitFor();
System.out.println(exitVal);
}
}
When I now run java -cp bin JarRunner
, I get the output 2as expected.
当我现在运行时java -cp bin JarRunner
,我得到了预期的输出2。
回答by Aviad Gispan
Go to your workspace library, workspace.metadata.plugins\org.eclipse.e4.workbench and remove the workbench file :) . after that you can restart eclipse
转到您的工作区库,workspace.metadata.plugins\org.eclipse.e4.workbench 并删除工作台文件 :) 。之后你可以重新启动eclipse