#I need help on how to display the output

1 messages ยท Page 1 of 1 (latest)

carmine sigil
#

change your while loop to a do..while or even just a while(true) and break when the stream ends

also, it is much preferred to use something like a Runnable and Executors (the ForkJoinExecutor is fine here) instead of Thread directly

oblique island
#

how does the code look like with those runnable and executors. i'm in a point that my brain isn't working anymore at 2am

carmine sigil
#

then go to bed

#

that is the best advice i can give you right now ๐Ÿ˜ƒ

oblique island
#

welp, at least i have able to do some "linking" with the fingerprint hardware and the software

#

but yea, need to sleep since i have class later. but i also need to fix this that it will output the stream inside java

carmine sigil
#
                    // Append the line to the text area on the Event Dispatch Thread
                    final String output = line + "\n";
                    SwingUtilities.invokeLater(() -> textArea.append(output));
                }
oblique island
#

I'll be trying this out later, I really need to go to sleep.

oblique island
#

I updated the code a bit.

public void startLogic(String code) {
        String pythonScriptPath = "python/adafruit/hook/add_fingerprint.py";
        String terminalCommand = "python3 " + pythonScriptPath + " " + code;

        try {
            // Start the process
            Process process = Runtime.getRuntime().exec(terminalCommand);

            // Consume the output stream
            Thread stdOutThread = new Thread(new StreamConsumer(process.getInputStream(), textArea));
            stdOutThread.start();

            // Optionally, consume the error stream
            Thread stdErrThread = new Thread(new StreamConsumer(process.getErrorStream(), textArea));
            stdErrThread.start();

            // Wait for the process to complete
            int exitCode = process.waitFor();
            if (exitCode !=  0) {
                // Handle non-zero exit codes
            }

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
    private static class StreamConsumer implements Runnable {
        private final InputStream is;
        private final JTextArea textArea;

        public StreamConsumer(InputStream is, JTextArea textArea) {
            this.is = is;
            this.textArea = textArea;
        }

        @Override
        public void run() {
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    final String finalLine = line; // Declare final variable for use inside lambda
                    SwingUtilities.invokeLater(() -> textArea.append(finalLine + "\n"));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
#

if the fingerprint sensor is not atteched, i can see the output inside the JTextArea like this

#

but if the fingerprint sensor is attached and just sitting around, the software hangs for some reason

#

update, it worked (?) but it only appeared after the python process ended

carmine sigil
#

since it's been 2 decades since i've used process builder, my searching turned up this snippet of code, so make of it what you will
you can redirect stderr to stdout : processBuilder.redirectErrorStream(true);
and then

   stdout.transferTo(System.out); // Print to console
   // Alternatively, capture the output:
   // ByteArrayOutputStream output = new ByteArrayOutputStream();
   // stdout.transferTo(output);
}
oblique island
#
public void startLogic(String code) {
        int ID = 100;
        String pythonScriptPath = "python/adafruit/hook/add_fingerprint.py";
        String terminalCommand = "python3 " + pythonScriptPath + " " + ID;

        try {
            // Start the process
            Process process = Runtime.getRuntime().exec(terminalCommand);

            // Consume the output stream
            Thread stdOutThread = new Thread(new StreamConsumer(process.getInputStream(), textArea));
            stdOutThread.start();

            // Optionally, consume the error stream
            Thread stdErrThread = new Thread(new StreamConsumer(process.getErrorStream(), textArea));
            stdErrThread.start();

            // Wait for the process to complete
            int exitCode = process.waitFor();
            if (exitCode ==  0) {
                // Handle zero exit codes
                JOptionPane.showMessageDialog(null, "Fingerprint registration success.");
                dispose();
            }
            else {
                JOptionPane.showMessageDialog(null, "Fingerprint not captured. Please try again.");
                dispose();
            }

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
#

also the other one

private static class StreamConsumer implements Runnable {
        private final InputStream is;
        private final JTextArea textArea;

        public StreamConsumer(InputStream is, JTextArea textArea) {
            this.is = is;
            this.textArea = textArea;
        }

        @Override
        public void run() {
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    final String finalLine = line; // Declare final variable for use inside lambda
                    SwingUtilities.invokeLater(() -> textArea.append(finalLine + "\n"));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
carmine sigil
#

i just gave you the appropriate hints -- actually implementing it is up to you (or do a similar search) ๐Ÿ˜€

oblique island
#

It's been a while. There is a lot of progress happened with the enrolling of fingerprint which is already finished but I hit another roadblock which involves of two processes conflicting each other

#

The flow of the system

  1. When the Main class starts, the fingerprintLoop() function also starts indefinitely until an interruption is called (which is starting up AddFingerprint class)
  2. When AddFingerprint class is called, the fingerprintLoop() will stop immediately before AddFingerprint reached it's own public static void main (String[] args) so that the ProcessBuild doesn't lock up the fingerprint hardware from other process accessing it (which is the adding of fingerprint).
  3. After the action from the AddFingerprint class regarding if it's success or fail, there is a way to call fingerprintLoop() from the Main class so that it can start reading fingerprint again.
#

Since I've been doing this since last night and it's already 2:20am, I'll be stopping here.

carmine sigil
#

i'm going to be brutally honest with you -- this is NOT the way to do what you want: you have too many conflicting threads, not to mention that you're trying to control an external process via those threads

what you need to do is either write the whole project in Python or Java so that the application can talk directly and correctly to the interface

oblique island
#

But to be honest, I already knew what's going to happen with this. It's got to the point that the progress is almost at 50% of the system