4

Currently, I am trying to read the console output of my program when there is a method call. How it works is, my Java program calls the JNI wrapper to invoke the C++ method call. My C++ is using std::cout. The payload.invoke will invoke my c++ library API. Inside the c++ API there are a few cout statements. That is the statement I want to read it as a variable in Java.

The current PrintStream only supports out, print, println and printf.Each time when there is a method call, there are bunch of logs being printed out in command prompt. Now, I would like my java program to read those output which my C++ prints out.

This is my current Java code:

public void execute() {
    try {
        Matcher m = Pattern.compile("([^()]*)[(]([^()]*)[)]").matcher(getPayloadString());
        m.find();
        boolean passed;
        if (m.group(2).isEmpty()) {
            // this is where it invoke the method
            passed = (boolean) payload.invoke(testcase);
            System.out.println("Boolean is: " + passed + "\n"); 
        }else {
            passed = (boolean) payload.invoke(testcase, ObjectCreator.convertParameters(m.group(2)));
            System.out.println("Boolean2 is: " + passed + "\n");

        }
        String output = passed ? "PASS" : "FAIL";
        // I want to use the console output string variable here -> xxxx
        TestCase.printResult(payload.getName(), output, m.group(2), xxxx, "");
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

Are there any ways to do it? Previously I tried using one of the tutorial from here. but that seems not working. Can someone help me out with this please. Thanks in advance.

13
  • This might be what you're looking for; https://stackoverflow.com/questions/8708342/redirect-console-output-to-string-in-java Commented Oct 20, 2020 at 9:48
  • @Fecid I am not really sure, because I went through it just now, but I am not really sure how can I use it because, the link is based on system.out outputs. Commented Oct 20, 2020 at 9:54
  • Wouldn't it suffice to just store all output in a variable before printing? Then you could just read these stored values. Commented Oct 20, 2020 at 10:42
  • If you see in the code there, I am not using neither the System.out or print(). I am programatically calling the method using .invoke. I am not really sure how can I capture the output when the invoke method is triggered? Commented Oct 20, 2020 at 11:07
  • 1
    @RaajLokanathan, yes, that is what I had in mind. Another option would be to have the native code print to a std::stringstream and then return std::stringstream::str() from that function? Commented Oct 27, 2020 at 7:09

2 Answers 2

0

If you have full control of the .cpp codebase it is possible for you to change the std::cout to become a ostream& instead.

By referencing a base class you can easily switch the implementation.

Do a JNI-Stream whichh extends the ostream& and just overwrite the operators you need to send JNI wrapped callbacks to java.

Then depending on if you run native or Java style use your JNI-Stream instead of the std::cout.

I would do something as seen in the link below to package and send the data to Java:

JNI - How to callback from C++ or C to Java?

that is my 2 cents.

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

Comments

-1

Read carefully the JNI tutorial.

https://www.baeldung.com/jni

Follow the tutorial. Basically you need to create a java class with a static block to load the C++ library:

static {
    System.loadLibrary("native");
}

Then you can create your own static java method and invoke a method on the library.

If what you need is read the output from a system call, it's a different matter. This can help you:

Java Runtime.getRuntime(): getting output from executing a command line program

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.