Launch() exec() command on linux

I want to send a terminal command from my sketch using launch() or exec() but i am facing issues in both cases:
when i use

launch("echo \"Hai\" >> /dev/usb/lp0");

looks like it tries to open a folder instead of executing the command
and i get the error
“The file or folder /home/user/processing-4.3/echo “Hai” >> /dev/usb/lp0 does not exist.”

when i use:

exec("echo \"Hai\" >> /dev/usb/lp0");

i get the following exeption:
“java.lang.RuntimeException: Exception while attempting echo “Hai” >> /dev/usb/lp0
at processing.core.PApplet.exec(PApplet.java:3193)
at SerialCallResponse.mousePressed(SerialCallResponse.java:45)
at processing.core.PApplet.mousePressed(PApplet.java:2425)
at processing.core.PApplet.handleMouseEvent(PApplet.java:2371)
at processing.core.PApplet.dequeueEvents(PApplet.java:2261)
at processing.core.PApplet.handleDraw(PApplet.java:2104)
at processing.awt.PSurfaceAWT$9.callDraw(PSurfaceAWT.java:1386)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:356)
Caused by: java.io.IOException: Cannot run program “echo “Hai” >> /dev/usb/lp0”: error=2, No such file or directory
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1143)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1073)
at java.base/java.lang.Runtime.exec(Runtime.java:594)
at java.base/java.lang.Runtime.exec(Runtime.java:453)
at processing.core.PApplet.exec(PApplet.java:3191)
… 7 more
Caused by: java.io.IOException: error=2, No such file or directory
at java.base/java.lang.ProcessImpl.forkAndExec(Native Method)
at java.base/java.lang.ProcessImpl.(ProcessImpl.java:314)
at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:244)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1110)
… 11 more”

I am running Linux Kde Neon.
below is the code i am trying to run:

void setup() {
  size(200, 200);
}

void draw() {
  // draw() must be present for mousePressed() to work
}

void mousePressed() {
  println("Opening Process_4");
  try{
    exec("echo \"Hai\" >> /dev/usb/lp0");
    //launch("echo \"Hai\" >> /dev/usb/lp0");
  }catch (RuntimeException e) {
    e.printStackTrace();
  }
}

I appreciate any help

I found a solution (with chatGPT :smiling_face:).

String command = "echo 'Hai' >> /dev/usb/lp0"; //terminal command

void setup() {
  size(200, 200);
  
}

void draw() {
  // draw() must be present for mousePressed() to work
}

void mousePressed() {
  println("Opening Process_4");
  try{
    exec("bash", "-c", command);
  }catch (RuntimeException e) {
    e.printStackTrace();
  }
}

launch() doesn’t seem to work but exec() does.
hope it might be useful for someone facing the same issue.

1 Like