http://qs321.pair.com?node_id=1162809

rgwest61 has asked for the wisdom of the Perl Monks concerning the following question:

My basic question - "How to call existing Java method from Perl" has been asked and answered before, but I am confused by the various different responses. Instead of describing the Java code implemented I have provided code snippets (with possible syntax errors) providing the key elements below.

Public Class ConnProperties{ private String driver; private String url; private String username; private String password; public ConnProperties(){ setDriver(null); setUrl(null); setUsername(null); setPassword(null); } ... attribute getter and setter methods } Public Class ConnPropertiesInterface(){ public ConnPropertiesInterface(){ super(); } public ConnProperties getProperties(String appID){ ConnProperties connProps = new ConnProperties(); ... Get Connection Properties from configuration file based on ... appID, update ConnProperties object with returned ... values such as connProps.setDriver(result.getString("driver")); connProps.setUrl(result.getString("url"); return connProps; } }

From existing Perl applications I need to be able to ultimately call the getProperties() method within class ConnPropertiesInterface and to decompose the returned ConnProperties object attributes to be used for connecting to an Oracle database. I have generated a jar file of the existing Java classes so that existing Java applications can successfully call this same method, now I just need to do the same from existing Perl applications. Being very new to Perl programming I am not sure the most correct/efficient way to accomplish this task. Thanks ahead of time for your help/assistance.

  • Comment on Dazed/Confused from previous posts on how to call Java method from Perl
  • Download Code

Replies are listed 'Best First'.
Re: Dazed/Confused from previous posts on how to call Java method from Perl
by Paladin (Vicar) on May 11, 2016 at 22:00 UTC
    I've never tried it myself, but a quick look at CPAN shows Inline::Java that may do what you need.
Re: Dazed/Confused from previous posts on how to call Java method from Perl
by iguanodon (Priest) on May 12, 2016 at 23:28 UTC
    Get Connection Properties from database

    Are your database connection properties stored in... a database? I can sort of see why you might want to keep the connection info in one place and use it from both Perl and Java. But if the Java programs connect to the database to get this info, IMO it would be easier to just have the Perl program make a DBI connection to get it as well. I admit I may not really understand the goal here.

      My apologies the database connection attributes (driver, url, username, password) are contained in a configuration file which is read and the associated attributes for the provided application id and retrieved and stored in the ConnectionProperties object. The Username and password values are encrypted and stored in the configuration file for security reasons. They are then decrypted before storing in the ConnectionProperties object. I still need detailed/specific code/instructions on what is needed to call the indicated class method. Thanks again for your time.

        Can you parse the config file and decrypt the credentials from the Perl program? If you absolutely need to get the connection attributes from the Java object I have to echo what Paladin said - look into Inline::Java, but I haven't done this myself.
Re: Dazed/Confused from previous posts on how to call Java method from Perl
by afoken (Chancellor) on May 12, 2016 at 20:45 UTC

    A slightly different, very generic way of doing it:

    Define and implement an interface in both languages, communicate using the interface. You probably want to use a client-server model, but peer-to-peer may also work.

    For communication, Unix domain sockets and TCP/IP sockets (usually via localhost) are quite easy to use from many languages. (Ab)using HTTP is also an option. To encode data, consider using JSON, it's lightweight and has encoders and decoders implemented in about 60 languages. YAML or XML may also be useful. There are also at least two spec for JSON-based Remote Procedure Calls.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Dazed/Confused from previous posts on how to call Java method from Perl
by rgwest61 (Initiate) on May 13, 2016 at 18:16 UTC

    I have tried to implement the below Perl code based on a response provided from another thread for which the requestor asked the same basic question that I am trying to resolve - "How to call Java methods from Perl:.

    #! /usr/bin/perl -w use strict; use warnings; use Inline; use Inline ( Java => 'STUDY', STUDY => ['home.rwest.workspace....'], AUTOSTUDY => 1, ); use Inline::Java gw(caught);

    When I attempt to execute this code I get the following: Error: You have specified 'Java' as an Inline programming language. I currently only know about the following languages: C, Foo, foo ... Why am I getting this Error? I saw a response from an internet search related to this error that Inline::Java needs to be installed. Can someone explain the entire process required to resolve this issue?

      Error: You have specified 'Java' as an Inline programming language. I currently only know about the following languages: C, Foo, foo ...

      Is Inline::Java installed?

      use Inline; use Inline ( Java => 'STUDY', STUDY => ['home.rwest.workspace....'], AUTOSTUDY => 1, ); use Inline::Java gw(caught);

      There are at least two things wrong with that piece of code:

      • You need to use Inline only once.
      • There is a typo after Inline::Java, you want qw(), not gw().

      Have a look at the documentation of Inline::Java, it's full of examples. Also look at the test scripts of Inline::Java, they show how Inline::Java is actually used.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)