#I need help on how to display the output
1 messages ยท Page 1 of 1 (latest)
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
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

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
// Append the line to the text area on the Event Dispatch Thread
final String output = line + "\n";
SwingUtilities.invokeLater(() -> textArea.append(output));
}
I'll be trying this out later, I really need to go to sleep.
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
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);
}
how does the code look like when i apply it to my software? also i did some few more updates to the logic
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();
}
}
}
i just gave you the appropriate hints -- actually implementing it is up to you (or do a similar search) ๐
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
- When the
Mainclass starts, thefingerprintLoop()function also starts indefinitely until an interruption is called (which is starting upAddFingerprintclass) - When
AddFingerprintclass is called, thefingerprintLoop()will stop immediately beforeAddFingerprintreached it's ownpublic 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). - After the action from the
AddFingerprintclass regarding if it's success or fail, there is a way to callfingerprintLoop()from theMainclass 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.
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
If you mean rewrite the entire project to Python, then I'll be malding how to use their libraries for the first time. If I write it purely in Java, there is no library for the fingerprint hardware I use but there is Jython that I tried before but for some reason it bugs out when it is deployed to the RPi 3 than on the laptop.
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