从Shell脚本调用Java方法 [英] Calling Java Methods from Shell Scripts

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

问题描述

如何从shell脚本中执行Java方法?

How to execute a Java method from inside shell scripts?

推荐答案

你只能调用 main 方法。设计你的方法,以便它调用你想要的方法。

You can only call the main method. Design your main method such that it calls the method you want.

当我说 call main 方法,您没有显式调用它。当你调用它时,它是java程序的唯一入口点。

When I say call main method, you don't explicitly invoke it. It's the only entry point to a java program when you invoke it.

如果你的类看起来像:

package com.foo;

public class Test {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

您可以使用以下命令行来调用 main 来自你可以找到的目录 com / foo / Test.class (如果你在 classes 远程显示的结构中的目录):

You can use the following command line to invoke the main from within the directory where you can find com/foo/Test.class (If you're in the classes directory in the structure shown far below):

java com.foo.Test

如果你想从另一个(见下面的目录结构)目录中这样做,那么你必须设置类路径。

If you want to do so from a different (See the directory structure far below) directory, then you'll have to set classpath.

java -cp /path/to/classes com.foo.Test

为清晰起见,假设以下目录结构。

Assume the below directory structure for clarity.

-path
    -to
        -classes
            -com
                -foo
                    >Test.class

这篇关于从Shell脚本调用Java方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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