This discussion is archived
3 Replies Latest reply: Dec 20, 2012 2:52 PM by hinkmond RSS

java.io.IOException: Invalid argument

A Tael Journeyer
Currently Being Moderated
Hello,
I have written a small piece of Java code, well I borrowed it from a blog and I just can't get it to work. Note that I am a novice developer and even more novice Embedded developer.
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package atael;

import java.io.FileWriter;
import java.io.IOException;

/**
 *
 * @author atael
 */
public class PiGPIO {

    static final String GPIO_OUT = "out";
    static final String GPIO_ON = "1";
    static final String GPIO_OFF = "0";
    static final String GPIO_CH00="0";
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            
            /*** Init GPIO port for output ***/
            
            // Open file handles to GPIO port unexport and export controls
            FileWriter unexportFile = new FileWriter("/sys/class/gpio/unexport");
            FileWriter exportFile = new FileWriter("/sys/class/gpio/export");

            // Reset the port
            unexportFile.write(GPIO_CH00);
            unexportFile.flush();
            
            // Set the port for use
            exportFile.write(GPIO_CH00);   
            exportFile.flush();

            // Open file handle to port input/output control
            FileWriter directionFile =
                    new FileWriter("/sys/class/gpio/gpio"+GPIO_CH00+"/direction");
            
            // Set port for output
            directionFile.write(GPIO_OUT);
            directionFile.flush();
            
            /*** Send commands to GPIO port ***/
            
            // Open file handle to issue commands to GPIO port
            FileWriter commandFile = new FileWriter("/sys/class/gpio/gpio"+GPIO_CH00+
                    "/value");
            
            // Loop forever
            while (true) {
                
                // Set GPIO port ON
                commandFile.write(GPIO_ON);
                commandFile.flush();
        
                // Wait for a while
                java.lang.Thread.sleep(200);
        
                // Set GPIO port OFF
                commandFile.write(GPIO_OFF);
                commandFile.flush();
                
                // Wait for a while
                java.lang.Thread.sleep(200);
            }

        } catch (IOException | InterruptedException exception) {
            exception.printStackTrace();
        }
    }
}
It complies nicely and I transfer it to my RaspBerry Pi. When I try to execute my application like this:

root@raspberrypi:~# java -jar /home/pi/atael.jar

I get the following error:
java.io.IOException: Invalid argument
        at java.io.FileOutputStream.writeBytes(Native Method)
        at java.io.FileOutputStream.write(FileOutputStream.java:318)
        at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
        at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291)
        at sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:295)
        at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:141)
        at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:229)
        at atael.PiGPIO.main(PiGPIO.java:35)
I am using the following version of Java SE:
root@raspberrypi:~# java -version
java version "1.7.0_10"
Java(TM) SE Embedded Runtime Environment (build 1.7.0_10-b18, headless)
Java HotSpot(TM) Embedded Client VM (build 23.6-b04, mixed mode)
Any idea what I am doing wrong?

--Any                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points