Auto find serial port, script problem

Hi
I have been working on this for a couple of days now and cant figure it out
I´m constantly sending out data by the arduino serial port.
this is what i have got so far:

import processing.serial.;
import cc.arduino.
;

Serial Port;
int i = 0;
int c = 0;
int spnr = Serial.list().length;

void setup() {
println(Serial.list());
println("Serial Port nr = " + spnr);
Port = new Serial(this, Serial.list()

, 115200);
}

void draw() {
  if (i > spnr) {
      println("Cant fined a connection");
      Port.clear();
      Port.stop(); 
      } 
      
   println("Testing port " + Serial.list()[c]);
   i++;
   delay(2000);

   
    if (Port.available() > 0) {
      Port.clear();
      Port.stop();
      println("Found");
      println(Port);
      
    } else {
      c++;
    }
}


If i use the bluetooth connection to the arduino I get this error directly:

gnu.io.PortInUseException: Unknown Application
	at gnu.io.CommPortIdentifier.open(CommPortIdentifier.java:354)
	at processing.serial.Serial.<init>(Serial.java:139)
	at processing.serial.Serial.<init>(Serial.java:105)
	at serial_test3.setup(serial_test3.java:34)
	at processing.core.PApplet.handleDraw(PApplet.java:1608)
	at processing.core.PApplet.run(PApplet.java:1530)
	at java.lang.Thread.run(Thread.java:680)
Exception in thread "Animation Thread" java.lang.RuntimeException: Error inside Serial.<init>()
	at processing.serial.Serial.errorMessage(Serial.java:591)
	at processing.serial.Serial.<init>(Serial.java:151)
	at processing.serial.Serial.<init>(Serial.java:105)
	at serial_test3.setup(serial_test3.java:34)
	at processing.core.PApplet.handleDraw(PApplet.java:1608)
	at processing.core.PApplet.run(PApplet.java:1530)
	at java.lang.Thread.run(Thread.java:680)


if I use a USB connection to the ardunio it thinks it found something on the first serial port.

You might consider some exception handling. The Processing application is not going to be able to connect to a port that is already in use.

How can I tell if a port is used without attempting to connect to it?

How can I tell if a port is used without attempting to connect to it?

I don't think you can. You can, though, use a try block around the attempt to connect, and then have a catch block that catches the exception, if one is thrown. The problem you are seeing is that the exception is thrown, but nothing catches it.