Java 代码的返回值
有一个 Java 类,它创建 POST 请求并将其发送到 servlet。 类文件(测试)的主要方法如下所示:
public static void main(String[] args) throws IOException {
// Code logic goes here...
// No return Statement
}
这是从 KornShell (ksh) 脚本调用的,如下所示:
retcode=`$CLK_JAVA_PATH -cp $CLASSPATH test ${PASSWORD} ${HOSTNAME} ${TOOLSET}`
if [ $? != "0" ];then
echo "ERROR:
echo "${retcode}"
else
echo "${SCRIPT} Success"
fi
retcode
始终具有值“2”,无论代码是否失败或成功。 我的问题是,既然我的 main 方法的返回类型是“void”,为什么代码会返回一些值?
There is a Java class which creates a POST request and sends it to a servlet.
The main method of the class file (test) looks something like this:
public static void main(String[] args) throws IOException {
// Code logic goes here...
// No return Statement
}
This is called from a KornShell (ksh) script something like this:
retcode=`$CLK_JAVA_PATH -cp $CLASSPATH test ${PASSWORD} ${HOSTNAME} ${TOOLSET}`
if [ $? != "0" ];then
echo "ERROR:
echo "${retcode}"
else
echo "${SCRIPT} Success"
fi
retcode
always has the value "2" independent of if the code fails or succeeds.
My question is since the return type of my main method is "void" why is the code returning some value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Java 应用程序的返回值不是其
main
方法的返回值,因为 Java 应用程序不一定在main
时结束方法已经执行完毕。相反,当不再有非守护线程运行或
System.exit()
被调用。并且
System.exit()
也是指定返回值的唯一方法:传递给System.exit()
的参数将用作 JVM 的返回值大多数操作系统上的进程。因此,以这样的方式结束
main()
方法将确保两件事:
main
末尾时,您的 Java 应用程序确实退出,并且The return value of a Java application is not the return value of it's
main
method, because a Java application doesn't necessarily end when it'smain
method has finished execution.Instead the JVM ends when no more non-daemon threads are running or when
System.exit()
is called.And
System.exit()
is also the only way to specify the return value: the argument passed toSystem.exit()
will be used as the return value of the JVM process on most OS.So ending your
main()
method with this:will ensure two things:
main
is reached andJava 程序不会像 C 和 C++ 那样通过从
main
返回值来将退出代码返回给操作系统。您可以通过调用System.exit(code);
来退出程序并指定退出代码,例如:Java programs do not return an exit code back to the operating system by returning a value from
main
, as is done in C and C++. You can exit the program and specify the exit code by callingSystem.exit(code);
, for example:这将返回错误代码 0(一切正常)。
System.exit 文档
This returns error code 0 (everything went fine).
System.exit Doc
使用
此选项将使您的应用程序能够控制操作系统看到的返回值。
Use
this will give your app control over the return value seen by the OS.
您的程序在退出后总是返回返回代码。在正常程序中,如果不指定返回码,它将返回零(这包括将返回类型设置为
void
)。然而,Java 喜欢与众不同! Java 不会返回您在 Main 方法中返回的返回代码,但当 JVM 退出时,它会返回一些返回代码(这考虑了多线程程序),并且会返回
System.Exit(returnCode); 的内容。
调用指定。Your program always returns a return code after exiting. In normal programs, if you do not specify a return code, it will return zero (this includes setting the return type to
void
).Java, however, likes to be special! Java won't return the return code you return at the Main method, but it'll return some return code when the JVM exits (this accounts for multithreaded programs), and will return what a
System.Exit(returnCode);
call specifies.您没有获得退出状态,这就是
$?
包含的内容。无论写入System.out
中的内容如何,您都会获得标准输出。You're not getting the exit status, that's what
$?
contains. You're getting standard out, whatever is written toSystem.out
.