1

We have a project (say, Foo) which has some public static methods, some of them return String. Now, I need to access that project's jar from command-line and call one of those public static String methods to get that String.


EDIT 1:

I need to pass that returned string as an argument to a shell script. Something like this:

./my_bash_script.sh <value returned from jar> <more arguments> 

How do we do it?

Tried to call the jar from command-line, but that didn't help.

java -cp Foo.jar full.package.name.ClassName

Error: Could not find or load main class jar

It was expected, since the class doesn't have a Main function.

Also tried to add a wrapper around the Foo.jar. Like, added this jar (Foo.jar) in my other project as a dependency, and called public static String method from Foo.jar. Well, this works, but I have to return a String outside Java application and Main has to be void.

Can anyone please help me with this?

Please ask for more explanation, if I have left out any detail, before down-voting.

Bhushan
  • 18,329
  • 31
  • 104
  • 137
  • possible duplicate of [Can I invoke a java method other than main() from the command line?](http://stackoverflow.com/questions/8054703/can-i-invoke-a-java-method-other-than-main-from-the-command-line) – specializt Jul 24 '14 at 21:31
  • 2
    What does "I have to return a String outside Java application" mean? If you have another application that needs to read from this Java application, does it take stdin/out which could be redirected? – Adrian Pang Jul 24 '14 at 21:32
  • @specializt: Sadly, I cannot use Groovy. – Bhushan Jul 24 '14 at 21:34
  • Isn't Main() always void? You can print your "string" to stdout and read it. How are you planning to get hold of that "string"? – TJ- Jul 24 '14 at 21:34
  • @AdrianPang: Details added – Bhushan Jul 24 '14 at 21:37
  • Execute your jar (via the wrapper). Store the stuff in a file (or any stream). Pipe it for next steps. Do you have control over the shell script? http://unix.stackexchange.com/questions/13326/how-to-pipe-output-from-one-process-to-another-but-only-execute-if-the-first-has – TJ- Jul 24 '14 at 21:39
  • @TJ-: Yes, I do have control over shell script – Bhushan Jul 24 '14 at 21:41
  • 1
    perfect. Pipe the output of the wrapper execution. – TJ- Jul 24 '14 at 21:42

3 Answers3

1

You just need to create a main method that passes a command line parameter to the method and then does a System.out.println(result).

A more general solution would require a wrapper that used reflection to invoke the right method and print out the result.

You could do that via something like this:

public class InvokeMethod {
    public static void main(String[] args) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        String clazz = args[0];
        String method = args[1];
        String[] methodArgs = new String[args.length - 2];
        Class<?>[] methodParam = new Class[args.length - 2];
        for (int i = 2; i<args.length; i++) {
            methodArgs[i-2] = args[i];
            methodParam[i-2] = String.class;
        }

        Class<?> c = Class.forName(clazz);
        Method m = c.getMethod(method, methodParam);
        String result = (String) m.invoke(methodParam);
        System.out.println(result);
    }
}

Compile that and package it into invoke.jar and then you can just do java -cp invoke.jar:your_real_jar.jar InvokeMethod some.class.Here method_name arg1 arg2 arg3...

Alcanzar
  • 16,985
  • 6
  • 42
  • 59
  • Thanks for the respone Alcanzar. Looks like specializt's answer is more straight forward. +1 for your reply. – Bhushan Jul 24 '14 at 22:00
  • His is definitely easier, but if you have multiple times you have to write a wrapper, simpler just to write it once. – Alcanzar Jul 25 '14 at 03:27
1

the easiest solution would be : create & compile a new java console application as JAR, include your other JAR and

System.out.println

the output from it, easy as cake. After that you can start your new JAR with

java -jar FILE.jar

and have the output just the way you need it

specializt
  • 1,913
  • 15
  • 26
  • I was able to print out the output but forgot that stdout can be taken by shell as argument. +1 and accepted. – Bhushan Jul 24 '14 at 21:59
1

You can pass the output of the wrapper to your shell script.

Java

// -- Test.java ----
public class Test
{
    public static void main(String args[])
    {
        // Call the method from Foo.jar here
        System.out.println("Hello" + " World");
    }
}

Shell

# ----- test.sh ------
#!/bin/sh
grep "$1" input.txt

input.txt

# Sample Input
Line 1 : Hello World
Line 2 : Here is Hello World Again
Line 3 : This is not Hello + World
Line 4 : AB Hello World CD

Commands

javac Test.java
./test.sh "`java Test`" # Note the `

Sample o/p

Line 1 : Hello World
Line 2 : Here is Hello World Again
Line 4 : AB Hello World CD
TJ-
  • 14,085
  • 12
  • 59
  • 90