K
K
kaleban2020-01-28 15:34:41
Java
kaleban, 2020-01-28 15:34:41

Get the result of the method and save it to a variable?

There is such a code, I pass two parameters to the input and get the result of this method in the console, I need to save the result to a variable, how can I do it right? Please do not throw tomatoes, I will be glad for any help. The result of the screen method

5e302a3a08318847421026.png

public static String activation(String serialNumber, String keyName) throws IOException, InterruptedException, SQLException {
        LocalDate futureDate = LocalDate.now().plusMonths(12);
        String formattedDate = futureDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        String[] command =
                {
                        "cmd",
                };
        Process p = Runtime.getRuntime().exec(command);
        new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();
        new Thread(new SyncPipe(p.getInputStream(), System.out)).start();
        PrintWriter stdin = new PrintWriter(p.getOutputStream());
        stdin.println("C:\\tdes_ecb.exe " + serialNumber + " " + keyName + " " + formattedDate);
        stdin.close();
        int returnCode = p.waitFor();
        String code = Integer.toString(returnCode);
        return code;
    }

    static class SyncPipe implements Runnable {
        public SyncPipe(InputStream istrm, OutputStream ostrm) {
            istrm_ = istrm;
            ostrm_ = ostrm;

        }

        public void run() {
            try {
                final byte[] buffer = new byte[1024];
                for (int length = 0; (length = istrm_.read(buffer)) != -1; ) {
                    ostrm_.write(buffer, 0, length);

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        private final OutputStream ostrm_;
        private final InputStream istrm_;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sergey, 2020-01-28
@kaleban

kaleban probably the tdes_ecb.exe utility writes to stderr
add
+ " 2>&1"

stdin.println("c:\\Users\\Serguei\\echoargstostderr.cmd " + serialNumber + " "
        + keyName + " " + formattedDate + " 2>&1");

if it helps, mark it as a solution, please,
and yes, when you copied the code, you forgot to read that the process prints a disease
, this is done like this
StringBuffer processOutput = new StringBuffer();
    while ((line = stdoutBufferedReader.readLine()) != null) {
      processOutput.append(line);
    }
    StringBuffer processError = new StringBuffer();
    while ((line = stderrBufferedReader.readLine()) != null) {
      processError.append(line);
    }
    System.err.println("OUTPUT:" + processOutput.toString());
    System.err.println("ERROR	:" + processError.toString());
    int returnCode = process.waitFor();
    String code = Integer.toString(returnCode);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question