如何使用Inline :: Java连接到外部JVM?

时间:2014-09-08 16:04:04

标签: java perl jvm perl-module

如何使用Inline::Java连接到已在运行的外部JVM?

我正在尝试连接到正在运行的OSGi应用程序,因此我可以发送和接收对象,数组,列表等。据我所知,Inline::Java创建自己的JVM来运行Java代码;我需要弄清楚的是如何在JVM和运行应用程序的JVM之间进行通信。

我尝试使用RMI,并为客户端提供以下代码:

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import example.hello.Hello;

public class Client {

    private Client() {}

    public static void main(String[] args) {

        String host = (args.length < 1) ? null : args[0];
        try {
            Registry registry = LocateRegistry.getRegistry(host);
            Hello stub = (Hello) registry.lookup("Hello");
            String response = stub.sayHello();
            System.out.println("response: " + response);
        } catch (Exception e) {
            System.err.println("Client exception: " + e.toString());
            e.printStackTrace();
        }
    }

当我单独编译并运行它时,它可以工作,但是当我尝试使用Inline::Java将代码嵌入到Perl脚本中时,它会出错:

C:\Users\josesong\Documents\RMI_Test>perl hello.pl

A problem was encountered while attempting to compile and install your Inline
Java code. The command that failed was:
  "C:\Program Files\Java\jdk1.8.0_20\bin\javac.exe" -deprecation  -d "C:\Users\j
osesong\Documents\RMI_Test\lib\auto\hello_pl_5a5a" Client.java > cmd.out 2>&1

构建目录是:

C:\Users\josesong\Documents\RMI_Test\build\hello_pl_5a5a

错误消息是:

Client.java:3: error: package example.hello does not exist
import example.hello.Hello;
                    ^
Client.java:14: error: cannot find symbol
            Hello stub = (Hello) registry.lookup("Hello");
            ^
  symbol:   class Hello
  location: class Client
Client.java:14: error: cannot find symbol
            Hello stub = (Hello) registry.lookup("Hello");
                          ^
  symbol:   class Hello
  location: class Client
3 errors

要调试问题,我cd到构建目录并检查输出文件,如下所示:

 at hello.pl line 2.
BEGIN failed--compilation aborted at hello.pl line 2.

导致这种情况的原因是什么?

perl脚本:

use Inline (Java => Config => DIRECTORY => 'C:\Users\josesong\Documents\RMI_Test');
use Inline Java => 'C:\Users\josesong\Documents\RMI_Test\Client.java';

1 个答案:

答案 0 :(得分:1)

通过定义类路径并使用STUDY配置解决了这个问题,如下所示:

    use Inline (Java => Config => DIRECTORY => 'tmp');
    use Inline  Java => 'C:\Users\josesong\Documents\RMI_Test\Client.java',
    CLASSPATH => 'C:\Users\josesong\Documents\RMI_Test',
        STUDY => ['example.hello.Hello', 'example.hello.Server'];

    $obj = Client -> new();
    $obj -> main([]);