0

We got a requirement to call a static method inside a Java class via perl script and capture the return value of the method as we have to remove the syso statements in the Java code.

Sample Java class

public class SampleClass
{
   public static void main (String [] args) throws Exception   
   {
       var_1 = func(arg1,arg2);       
       System.out.print(var_1);
    }
    private static String func(final String arg1, final String arg2)
    throws Exception
    { 

        <--- code -->

        return value;
     }

     public static String SampleMethod(final String arg1, final String arg2)
     throws Exception
     {     
         var_2 = func(arg1,arg2);
         return var_2;
     }
}

Intially in perl we are calling the actual class like below

command = SampleClass Arg_1  Arg 2 
`$command`

Now the requirement is to call the SampleMethod and capture the return value to avoid reading the o/p from console.

Tried the below

command = SampleClass.SampleMethod Arg_1  Arg 2 
`$command`

But it failed saying unable to load main class in SampleMethod

Please advise.

5
  • 2
    "in perl we are calling the actual class like below: command = SampleClass Arg_1 Arg 2 " : This is not valid Perl syntax. Please clarify Commented Aug 10, 2021 at 21:13
  • 2
    "We got a requirement to call a static method inside a Java class via perl script.." : Please show the Perl script you use. Commented Aug 10, 2021 at 21:16
  • 1
    See also Calling a static jar class method from command line Commented Aug 10, 2021 at 21:26
  • 1
    I think you need to wrap the static method inside another class with a static main() that calls the original static method. See this answer Commented Aug 10, 2021 at 21:31
  • 1
    Inline::Java Commented Aug 11, 2021 at 3:30

1 Answer 1

2

You cannot call other static methods than main() in a Java class from Perl. A workaround is to wrap the static method inside another class with a static main() that calls the original static method, see also this answer. For example:

SampleClass.java:

public class SampleClass
{
    public static void main(String [] args) throws Exception
    {
       String result = SampleMethod("string1", "string2");
       System.out.println("Result: " + result);
    }

    public static String SampleMethod(final String arg1, final String arg2)
        throws Exception
     {
         return arg1 + " " + arg2;
     }
}

p.pl:

use feature qw(say);
use strict;
use warnings;

my $wrap_name = 'Wrapper';
my $source = <<"END_JAVA";
class $wrap_name {
    public static void main(String[] args) throws Exception {
        String result = SampleClass.SampleMethod("string1", "string2");
        System.out.print("Result: " + result);
    }
}
END_JAVA

my $fn = $wrap_name . '.java';
open ( my $fh, '>', $fn ) or die "Could not open file '$fn': $!";
print $fh $source;
close $fh;
system 'javac', $fn;
my $output = `java ${wrap_name}`;
say "Got output: '$output'";

You can then run:

$ javac SampleClass.java
$ perl p.pl
Got output: 'Result: string1 string2'
Sign up to request clarification or add additional context in comments.

36 Comments

Thanks for the reply, In the similar way can we call static SampleMethod which is outside main method in SampleClass.java which calls "func" as the requirement is to remove the Syso statements in main method.
I am not sure what you mean. The current script is calling SampleMethod() in SampleClass. Please clarify
my SampleClass.java is like public class SampleClass { public static void main (String [] args) throws Exception { var_1 = func(arg1,arg2); System.out.print(var_1); } private static String func(final String arg1, final String arg2) throws Exception { <--- code --> return value; } public static String SampleMethod(final String arg1, final String arg2) throws Exception { var_2 = func(arg1,arg2); return var_2; } }
Can I also be able to call SampleMethod through wrapper though it is not in main method ?
It Worked this time. Thank you very much for all the support provided.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.