Help for creating own library (add java file to sketch)

Hi erveryone,
i created a class called Adressbook. I copy the class inside the sketchbook.
created a new file and entered import Adressbook;
– no library found for Addressbook — I got as error.

What do I have to do to fix this problem? Or to you have an example (step by step) how to create a own library?
Thanks

1 Like

I assume you mean Addressbook and that the file you created contains Java source code, not a compiled class

  1. Name the file you created as Addressbook.java, must be the same name including capital letters as the class name.
  2. The sketch name must NOT be the same as the class
  3. The Addressbook.java file must be in the same folder as the sketch pde file.
  1. Do not add the import statement because it is not needed
  1. Theis error was created because of the import statement.
1 Like
1 Like

Thanks for you answer but it did not work for me.
File name is called Addressbook store in sketch folder.
Class Name Addressbook
When I save Addressbook.pde as Addressbook.java An error like this:
The sketch name had to be modified. Sketch names can only consist of ASCII characters and numbers (but cannot start with a number). They should also be less than 64 characters long.
And the new filename named Addressbook_java gets created.
here is mine class:

class Addressbook{
  private int id;
  private String name;

  public Addressbook(
    int id,
    String name) {
    this.name = name;
    this.id = id;
  }

  public String toString() {
    return id + " { " + name;
  }
  
  public String getName() {
    return name;
  }
  
  public int getID() {
    return id;
  }
}

class Adressbook {
  private String name;
  private Addressbook[] contacts;

  public Adressbook(String name) {
    this.name = name;
    this.contacts = new Addressbook[0];
  }

  public String showAll() {
    String output = "Adressbuch { " +name +"\n";
    for (int i = 0; i < contacts.length; i++) {
      output = output + contacts[i].toString() + "\n";
    }
    return output;
  }

  public String showByName(String name) {
    for (int i = 0; i < contacts.length; i++) {
      if (contacts[i].getName() == name) {
        return contacts[i].toString();
      }
    }
    return "";
  }

  public void addContact(Addressbook contact) {
    Addressbook[] contactsNew = new Addressbook[contacts.length + 1];
    for (int i = 0; i < contacts.length; i++) {
      contactsNew[i] = contacts[i];
    }
    contactsNew[contactsNew.length -1] = contact;
    contacts = contactsNew;
  }
  
  public Addressbook getContact(int id) {
    for (int i = 0; i < contacts.length; i++) {
      if (contacts[i].getID() == id) {
        return contacts[i];
      }
    }
    return null;
  }

  public void removeContact(int id) {
    Addressbook[] contactsCopy = new Addressbook[0];
    for (int i = 0; i < contacts.length; i++) {
      if (contacts[i].getID() != id) {
        Addressbook[] contactsCopyNew = new Addressbook[contactsCopy.length +1];
        for (int j = 0; j < contactsCopy.length; j ++) {
          contactsCopyNew[j] = contactsCopy[j];
        }
        contactsCopyNew[contactsCopyNew.length -1] = contacts[i];
        contactsCopy = contactsCopyNew;
      }
    }
    this.contacts = contactsCopy;
  }
}

and the new sketch 
import Addressbook;

public void setup() {
  Adressbook privat = new Adressbook("Privat");
  privat.addContact(new Contact(1, "ken Tern"));
  privat.addContact(new Contact(2, "jlkjlldk"));
  privat.addContact(new Contact(3, "lkjljlkj"));
  print(privat.showAll());
}

I know you me using without import but this did also not work.

All Contact should be Addressbook I know forgot to corrected.

OK the name you use to save the sketch MUST NOT BE THE SAME as any class you create inside the sketch

2 Likes

Just a note for future readers: Using a .java file in a sketch is also a way to include java in Processing, but it is not a library.

In Processing with the PDE, a “library” refers to a compiled jar file, which you can import with import. For most users these can be installed with Contributions Manager, and the jar is kept outside the sketch. Or the jar file – including ones that are not available through Contributions Manager – can be included directly in your sketch, for example in a \code folder.

1 Like