0

I am trying to use the GPIO pins on the Raspberry Pi. Currently I am using ProcessBuilder to execute the commands. However, whenever I run it it always says:

Exception in thread "main" java.io.IOException: Cannot run program "echo 18 > /sys/class/gpio/export": error=2, No such file or directory
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
        at Testing.initiate(Testing.java:32)
        at Testing.main(Testing.java:8)
Caused by: java.io.IOException: error=2, No such file or directory
        at java.lang.UNIXProcess.forkAndExec(Native Method)
        at java.lang.UNIXProcess.<init>(UNIXProcess.java:135)
        at java.lang.ProcessImpl.start(ProcessImpl.java:130)
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1022)
        ... 2 more

Here is the code I am using:

    ProcessBuilder pb = new ProcessBuilder("echo " + gpiopin + " > /sys/class/gpio/export");
    ProcessBuilder pb1 = new ProcessBuilder("echo out > /sys/class/gpio/gpio" + gpiopin + "/direction");
    ProcessBuilder pb2 = new ProcessBuilder("echo 1 > /sys/class/gpio/gpio" + gpiopin + "/value");
    pb.redirectErrorStream(true);
    pb1.redirectErrorStream(true);
    pb2.redirectErrorStream(true);
    Process ps = pb.start();
    Process ps1 = pb1.start();
    Process ps2 = pb2.start();

Why is this happening? Also, if there is a better way of doing this how?

Thanks

Edit: Seems like there are no more errors, but the commands are not running. Could someone please look at my code to make sure i'm not being stupid?

import java.util.Scanner;


public class Testing {
    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(System.in);
        initiate(18);

        while(true) {
            String input = scan.nextLine();

            if(input.equals("on")) {
                System.out.println("on");
                bellOn();
            }else if(input.equals("off")) {
                System.out.println("off");
                bellOff();
            }
        }
    }

    static int gpio = 18;
    static String gp = "" + gpio;
    static boolean initiated = false;

    public static boolean initiate(int gpiopin) throws Exception {
        gpio = gpiopin;
        ProcessBuilder pb = new ProcessBuilder("echo", gp, ">", "/sys/class/gpio/export");
        ProcessBuilder pb1 = new ProcessBuilder("echo", "out", ">", "/sys/class/gpio/gpio" + gp + "/direction");
        //ProcessBuilder pb2 = new ProcessBuilder("echo", "1", ">", "/sys/class/gpio/gpio" + gpiopin + "/value");
        pb.redirectErrorStream(true);
        pb1.redirectErrorStream(true);
        //pb2.redirectErrorStream(true);
        Process ps = pb.start();
        Process ps1 = pb1.start();
        //Process ps2 = pb2.start();
        initiated = true;
        return true;
    }

    public static boolean bellOn() throws Exception {
        if(initiated != true) {
            return false;
        }else{
            ProcessBuilder pb = new ProcessBuilder("echo", "0", ">", "/sys/class/gpio/gpio" + gp + "/value");
            pb.redirectErrorStream(true);
            Process ps = pb.start();
            return true;
        }
    }

    public static boolean bellOff() throws Exception {
        if(initiated != true) {
            return false;
        }else{
            ProcessBuilder pb = new ProcessBuilder("echo", "1", ">", "/sys/class/gpio/gpio" + gp + "/value");
            pb.redirectErrorStream(true);
            Process ps = pb.start();
            return true;
        }
    }
}
Tore Olsen
  • 2,153
  • 2
  • 22
  • 35
cheese5505
  • 962
  • 5
  • 14
  • 30
  • Found something here, maybe it could help you out? http://stackoverflow.com/questions/10735415/process-builder-gives-a-no-such-file-or-directory-on-mac-while-runtime-exec – ihsan Sep 05 '14 at 10:18
  • Seems there are no more errors, but I think I have done somethign wrong in the command. – cheese5505 Sep 05 '14 at 10:25
  • You should do something like `... new ProcessBuilder("echo " + gpiopin, ">", "/sys/class/...");`, I suppose. – ihsan Sep 05 '14 at 10:29
  • I just added more code to my main post - please havfe a look at it – cheese5505 Sep 05 '14 at 10:32
  • 2
    I do this in C++, but I just open /sys/class/gpio as a file. Can't you do this in Java? That would save all the messing around with ProcessBuilder.. – Greycon Sep 05 '14 at 11:42
  • Have you considered Pi4J? http://pi4j.com/ – Dropout Sep 05 '14 at 12:29
  • @Dropout I will use that as a back up plan as I don't really want to mess around with external libraries and would rather keep it in one piece of code. – cheese5505 Sep 05 '14 at 19:56

1 Answers1

0

This question is old but here is how I got my pins to work using the WiringPi library.

Referring to this question.

I have this code below which works when running in a SSH shell but not after the device reboots and runs as a cronjob after reboot.

public static String cmdCommands()
{
    try 
    {
        //blink LED; works in shell only
        Runtime r = Runtime.getRuntime();
        r.exec("gpio mode 4 out");
        r.exec("gpio write 4 1");
        Thread.sleep(500);
        r.exec("gpio write 4 0");
        Thread.sleep(500);
        return "done";          
    }
    catch(Exception e) 
    {
        return "Exception: " + e.getMessage();
    }
}

To fix this basically I had to use String[] and build the command. Very easy after struggling to find a fix

Working Code example here:

public static String cmdCommands()
{
    try 
    {
        //blink LED
        Runtime r = Runtime.getRuntime();
        r.exec(new String[] {"sudo", "gpio", "mode", "4", "out"});
        r.exec(new String[] {"sudo", "gpio", "write", "4", "1"});
        Thread.sleep(500);
        r.exec(new String[] {"sudo", "gpio", "write", "4", "0"});
        return "done";          
    }
    catch(Exception e) 
    {
        return "Exception: " + e.getMessage();
    }
}
Jonas
  • 1,105
  • 1
  • 15
  • 21