Running serial on Java

Hello.

Recently I've started a minor project using Arduino and I've hit a wall.
I'm trying for about 3 days to read a simple "Hello" from my board.
On the serial monitor everything works, but wanted to mannipulate my arduino with a java program.
Was working with this code I've found somewhere but I keep getting this error :
"Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
COM1
Exception : Unknown Application"

package serial;
import gnu.io.*;
import java.io.*;

public class Main {

public static void main(String[] s)
{
try
{
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("COM1");
if (portIdentifier.isCurrentlyOwned())
{
System.out.println("Port in use!");
}
else {
System.out.println(portIdentifier.getName());

SerialPort serialPort = (SerialPort) portIdentifier.open("ListPortClass", 300);
int b = serialPort.getBaudRate();
System.out.println(Integer.toString(b));
serialPort.setSerialPortParams(300, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
OutputStream mOutputToPort = serialPort.getOutputStream();
InputStream mInputFromPort = serialPort.getInputStream();
String mValue = "AT\r";
System.out.println("beginning to Write . \r\n");
mOutputToPort.write(mValue.getBytes());
System.out.println("AT Command Written to Port. \r\n");
mOutputToPort.flush();
System.out.println("Waiting for Reply \r\n");
Thread.sleep(500);
byte mBytesIn [] = new byte[20];
mInputFromPort.read(mBytesIn);
mInputFromPort.read(mBytesIn);
String value = new String(mBytesIn);
System.out.println("Response from Serial Device: "+value);
mOutputToPort.close();
mInputFromPort.close();
serialPort.close();
}}
catch (Exception ex)
{

System.out.println("Exception : " + ex.getMessage());
}

}
}

Any help is welcome.

Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
COM1
Exception : Unknown Application

I'm not fluent in Java, so I don't know what your problem is. In reading the code and the output, it looks to me like this:

System.out.println(portIdentifier.getName());

is what was responsible for the COM1 output. Is your Arduino really connected to COM1?

On my computer COM1 is always present, but it is never the port that my Arduino is connected to.

Putting some identifier data in front of strings helps to identify what the random data being output is. This:

System.out.println(Integer.toString(b));

looks like it prints the baud rate, and does not appear that it gets executed. So, the problem must be in the code between the statement that appears to work and the one that appears to not work:

SerialPort serialPort = (SerialPort) portIdentifier.open("ListPortClass", 300);
int b = serialPort.getBaudRate();

It seems unlikely that a successfully opened serial port would fail to return the connection rate that was negotiated, so it seems that either the output from portIdentifier.open could not be cast to a serial port or that portIdentifier.open is what caused the "Unknown Application" exception.

The obvious question, then, are what the input arguments to the CommPortIdentifier::open method mean. 300 appears to the (very low) baud rate. What about the string "ListPortClass"? Is the Arduino actually connected to COM1?