Need help with Output/ Input reading and writting exercise

How y'all doing today? I'm working on an exercise for class where i have to solve the next problem: In this task, we must create an application that displays the multiplication table for numbers from 1 to 10. To achieve this, we will create an application (which will be the child process) that generates the multiplication table for the number it receives as input. -The parent process will make 10 calls to the child process, passing as input the number of the table that the child must generate (1...10). -The child process will take this number and print the corresponding multiplication table. -The parent process will receive what the child prints and display it. (As far as i'm concerned i can't use threads) I'll leave the code for both parent and child (which is stored on a .jar) on a few screenshots (Padre is parent, and Hijo is child) It is not working since it never returns the childs part. Thx a lot!
No description
No description
32 Replies
JavaBot
JavaBot2mo ago
This post has been reserved for your question.
Hey @MEDICINE! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st2mo ago
What exactly do you mean with it not returning?
MEDICINE
MEDICINEOP2mo ago
it should print: Tabla del n: 1 * n = n 2 * n = 2n .... 10 * n = 10n ----------Proceso n finalizado---------- where n is a number from the parent class (on for loop) printing n for n =1, n= 2.... n = 10 but it is just printing Tabla del n: ----------Proceso n finalizado---------- It is basically not printing the text generated on child process
dan1st
dan1st2mo ago
The same for each of the numbers? I assume adding System.out.flush(); at the end of the child process doesn't do anything? btw you can configure the child process to inherit the IO of the parent process
MEDICINE
MEDICINEOP2mo ago
yeah, basically the parent process will create a new process on every loop where it sends a number (taken from variable i for the loop) and child will get the table for that number imma try
dan1st
dan1st2mo ago
idk whether you want to do that though
MEDICINE
MEDICINEOP2mo ago
How do i do that? Bruh i'm just new on coding and my teacher is garbage xd he just gives us a 300 PDF for every lesson and won't explain nothing
dan1st
dan1st2mo ago
new ProcessBuilder(...).inheritIO().start()
MEDICINE
MEDICINEOP2mo ago
okay now it says that it couldnt find the .jar
dan1st
dan1st2mo ago
Did you just add inheritIO()?
MEDICINE
MEDICINEOP2mo ago
i got it on the src yes
dan1st
dan1st2mo ago
that's probably why
MEDICINE
MEDICINEOP2mo ago
now it is launching that error, which wasn't even doing before
dan1st
dan1st2mo ago
The java command fails printing information that it doesn't find the JAR to stderr (not stdout) so reading from stdout didn't show that error The child JAR must be in the directory you are executing the parent in
MEDICINE
MEDICINEOP2mo ago
hold on, says exactly this: Error: Could not find or load main class .jar Caused by: java.lang.ClassNotFoundException: /jar
dan1st
dan1st2mo ago
Can you show your MANIFEST.MF?
MEDICINE
MEDICINEOP2mo ago
yeah
dan1st
dan1st2mo ago
of the child preferably
MEDICINE
MEDICINEOP2mo ago
No description
dan1st
dan1st2mo ago
Running it manually with java -jar works?
MEDICINE
MEDICINEOP2mo ago
lemme try
dan1st
dan1st2mo ago
Maybe you edited the code and made some typo so the java -jar was replaced by java /jar?
MEDICINE
MEDICINEOP2mo ago
it says: Error: Unable to access jarfile Hijo.jar hmm imma retry just in case okay now it doesn´t return nothing at all(but i guess it shouldn't since it just works when getting data from parent
MEDICINE
MEDICINEOP2mo ago
No description
MEDICINE
MEDICINEOP2mo ago
it just stays like that
dan1st
dan1st2mo ago
that likely means you are accessing it from the wrong directory the program requires input you need to enter the number after all
MEDICINE
MEDICINEOP2mo ago
yeah, but it has to come from the parent for loop
dan1st
dan1st2mo ago
if you execute it manually, you can just type the number in the console it's just System.in after all
MEDICINE
MEDICINEOP2mo ago
oh shit that is true okay it is working
MEDICINE
MEDICINEOP2mo ago
No description
MEDICINE
MEDICINEOP2mo ago
Okay i changed this on the ProcessBuilder on parent: Process procesoHijo = new ProcessBuilder("java", "-jar", "src/Hijo.jar").inheritIO().start(); basically just the path to the .jar. Now it is not throwing errors but the child is not returning nothing it does if i write a number on console but it is supposed to get its number automatically from parent's for loop okay i got it finally left this : Process procesoHijo = new ProcessBuilder("java", "-jar", "src/Hijo.jar").start(); and changed the way to send the number with BufferedWriter pos = new BufferedWriter(new OutputStreamWriter(procesoHijo.getOutputStream())); pos.write(String.valueOf(i)); pos.newLine(); pos.flush() now it works thx dude
JavaBot
JavaBot2mo ago
Post Closed
This post has been closed by <@440504711305625630>.

Did you find this page helpful?