This is a sample I wrote which runs under Windows with the RXTX library. It connects to the Arduino on COM3 and prints the character 'H' to it and closes the port. You can change the port parameters easily.
I had to insert a sleep instruction into the code as trying to talk to the serial port immediatly doesn't seem to work, it needs a second or so before it will send data.
You will need the RXTXcomm.jar in the classpath as well as rxtxSerial.dll
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.InputStream;
import java.io.OutputStream;
public class CommTest {
static InputStream input;
static OutputStream output;
static SerialPort port;
public static void main(String[] args) throws Exception {
// Open the serial port
CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier("COM3");
port = (SerialPort) portId.open("Arduino", 4000);
// Get the port input/output streams
input = port.getInputStream();
output = port.getOutputStream();
// Set the port parameters
port.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
// Wait for the serial port
Thread.sleep(1500);
// Write 'H' to the serial port
output.write(72);
// Close the serial port
port.close();
}
}