-3

I created a method (apiType) that executes a command on a linux server. The result of the method should be used in my main class for executing a class or other class, depending if the result is "app2" or "app3".

The result of the linux command (cat /opt/pcu/bin/version | grep PRODUCT | cut -d' ' -f2) is either "app2" or "app3"

So my question is how can I call and use the result of the method in my main class?

OR what I do wrong or I miss in the code?

public class Api {
    private final static String version = "v2";
    private final static String path = FileSystems.getDefault().getPath(".").toAbsolutePath().getParent().toString();

    public String apiType() throws Exception {
        try {
            Exec exec = new Exec();
            exec.setCommand("cat /opt/pcu/bin/version | grep PRODUCT | cut -d' ' -f2");
            exec.setLogPath(path);
            exec.run("apiType");
            exec.join(200);
            Vector<String> output = exec.getOutput(false);

            return output.get(0); //this should return "app2" or "app3"?!
        } catch (Exception e) {
            System.out.println("Failed to run command");
            throw e;
        }

    }

    public static void main(String[] args) {
        if (args.length == 0) {
            Date date = new Date();
            DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy/hh:mm:ss");
            System.out.println(
                    "\n\n==================================== TestAPI program ====================================\n");

            System.out.println("Date: " + dateFormat.format(date.getTime()) + "    " + version);
            System.out.println(
                    "\n==============================================================================================\n\n");

            Console console = System.console();
            String user = console.readLine("Username: ");
            String password = new String((console.readPassword("Password: ")));
            String testID = console.readLine("TestID: ");

            String pattern = "[Tt]?(\\d+)";
            Pattern pat = Pattern.compile(pattern);

            Matcher m = pat.matcher(testID);
            if (!m.matches()) {
                System.out.println("Test ID doesn't have a valid format");
                return;
            }

            testID = m.group(1);

            String csvFilePath = console.readLine("CSV Test File: ");


            if (????.equals("app2")) {

                TestApi2 test = new TestApi2(user, password, testID, csvFilePath);

                test.runTest();
            } else if (????.equals("app3")) {

                TestApi3 test2 = new TestApi3(user, password, testID, csvFilePath);

                test2.runTest();
            } else {
                System.out.println("Unknown");
            }
        } else if (args.length == 1 && args[0].equalsIgnoreCase("--help")) {

        }

    }
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
corny
  • 61
  • 1
  • 12
  • 1
    I can´t see where you call `apiType`. Of course you need to call it and use its return-value. – MakePeaceGreatAgain Feb 11 '20 at 10:28
  • 2
    You could make it `static`, then `main` (which is also `static`) could call it without having an object. `public static String apiType() {...}` – tevemadar Feb 11 '20 at 10:29
  • So you created a method that calls a system command, returns a `Vector` (in which universe does that still exist) and finds an app name and your question is how to call a method? – f1sh Feb 11 '20 at 10:32

1 Answers1

2

You need to intstantiate an Api

Api api = new Api();

then call the method and assign its returned value to a string

String apiType = api.apiType();

At that point you can use your newly created string apiType to check for the returned value

if (apiType.equals("app2")) {
    // ...
} else if (apiType.equals("app3")) {
    // ...
}

I'm assuming that the main method is only meant for testing the class itself, so I will assume you actually don't want to make apiType itself static.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95