从Java代码返回值 [英] Return value from a Java code

查看:446
本文介绍了从Java代码返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个Java类可以创建POST请求并将其发送到servlet。
类文件(test)的主要方法如下所示:

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
}

这是从KornShell(ksh)脚本调用的,如下所示:

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 始终具有值2独立如果代码失败或成功。
我的问题是,因为我的main方法的返回类型是void,为什么代码返回一些值?

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?

推荐答案

Java应用程序的返回值它的 main 方法的返回值,因为Java当 main 方法已经完成执行时,应用程序不一定会结束。

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's main method has finished execution.

相反,JVM结束时不再有非-daemon线程正在运行或 调用System.exit()

Instead the JVM ends when no more non-daemon threads are running or when System.exit() is called.

System.exit()也是指定返回值的唯一方法:传递给 Sys的参数tem.exit()将用作大多数操作系统上JVM进程的返回值。

And System.exit() is also the only way to specify the return value: the argument passed to System.exit() will be used as the return value of the JVM process on most OS.

结束 main()方法:

System.exit(0);

将确保两件事:


  • 当达到 main 结束时你的Java应用程序真正退出并且

  • 表示返回值为JVM进程为0

  • that your Java application really exits when the end of main is reached and
  • that the return value of the JVM process is 0

这篇关于从Java代码返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆