How to run a java jar file and get store the output in shell script variables

Pritam Kumar :

I have a java class, which has 3 methods and each method returns a String. I exported the .jar file out of that Java project. When I run the jar file from git bast using the command ./script1.sh, I get the output in the command window. My question is, how can I assign those values to the three variable that are in the script1.sh file. If I type echo ${"Source_branch"}, i should get output as "XXX_Source_Branch". Here is my Java code :

package com.main;

public class Engine {

    public static void main(String[] args) {
        try {
         getSourceBranch();
         getDestinationBranch();
         getDestEnv();
        } catch (Exception e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }
    }

    public static String getSourceBranch() {
        return "XXX_Source_Branch";
    }

    public static String getDestinationBranch() {
        return "XXX_Dest_Branch";
    }

    public static String getDestEnv() {
        return "XXX_Dest_Env";
    }
}

The name of the jar file is Engine.jar. And my script file is

runjar.sh

java -jar Engine.jar Source_branch Destination_brnach Destination_env

dan1st :

pass data from java to bash

If you want to save data from java to the overlaying String, you will need to print that to System.out and read them from the shell.

Java code:

System.out.println("your source branch")
System.out.println("your destination branch")

Shell code

out="$(java -jar Engine.jar)"
sBranch="$(echo "$out"|sed -n '1p' /dev/stdin)"
destBranch="$(echo "$out"|sed -n '2p' /dev/stdin)"

pass data from bash to java

If you hard-code the Strings, java will just use the Strings. If you want to change that, you will need to modify your java code.

option 1 - arguments

If you want to pass the strings in the script using java -jar Engine.jar Source_branch Destination_brnach Destination_env, you can use them in your main method with the args parameter.

For example, you can print the first String in your main method using

System.out.println(args[0]);

If you do that, please test if args.length is greater than the highest argument you are accessing.

option 2 - System properties

If you want to get parameters without accessing args(independent of your main method), you will need to use System properties.

At first, change the script to be like that:

java -jar -DSource_branch=yourSourceBranch -DDestination_branch=yourDestinationBranch -DDestination_env=yourDestinationEnv

Engine.jar

Note that -D. That indicates that you are passing System properties. The syntax for this is -D<key>=<value> and it should be located between java and -jar.

Also, you will need to use System.getProperty() to access it from anywhere in your code.

For example:

public static String getSourceBranch() {
    return System.getProperty("Source_Branch");
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=31422&siteId=1