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.

8
  • possible duplicate of Can I invoke a java method other than main() from the command line? Commented Jul 24, 2014 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? Commented Jul 24, 2014 at 21:32
  • @specializt: Sadly, I cannot use Groovy. Commented Jul 24, 2014 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"? Commented Jul 24, 2014 at 21:34
  • 1
    perfect. Pipe the output of the wrapper execution. Commented Jul 24, 2014 at 21:42

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

I was able to print out the output but forgot that stdout can be taken by shell as argument. +1 and accepted.
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...

2 Comments

Thanks for the respone Alcanzar. Looks like specializt's answer is more straight forward. +1 for your reply.
His is definitely easier, but if you have multiple times you have to write a wrapper, simpler just to write it once.
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

Comments

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.