有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在shell中执行java并等待,直到我得到返回代码

我现在正在执行如下java程序:

package com.test;

public class Test {

    public static void main(String[] args){
        execute();
    }

    public static String  execute(){
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "a";
    }
}

我想进行测试。在linux shell脚本中执行()方法,等待该方法返回并获取返回代码。但是main()方法的返回是无效的,那么我该如何从中获取返回代码或返回消息呢

有什么建议吗


我找到了一个解决方案:

package com.test;

public class Test {

    public static void main(String[] args){
          execute();
    }

    public static String  execute(){
        try {
            System.out.println("sleeping");;
            Thread.sleep(5000);
            Runtime.getRuntime().exit(100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "a";
    }
}

然后我的壳:

#!/bin/bash
java -cp test.jar com.test.Test
echo "The return code of the java application is $?"

我可以得到n的值,它在Runtime.getRuntime().exit(n)


共 (0) 个答案