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

**URL:** https://discourse.processing.org/t/help-for-creating-own-library-add-java-file-to-sketch/7130
**Category:** Libraries
**Created:** [January 3, 2019, 8:41am UTC](https://discourse.processing.org/t/help-for-creating-own-library-add-java-file-to-sketch/7130 "2019-01-03T08:41:47Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Wolfgang](https://avatars.discourse-cdn.com/v4/letter/w/54ee81/32.png) [@Wolfgang](https://discourse.processing.org/u/Wolfgang)
#### Post date: [January 3, 2019, 8:41am UTC](https://discourse.processing.org/t/help-for-creating-own-library-add-java-file-to-sketch/7130/1 "2019-01-03T08:41:47Z")

</div>

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

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [January 3, 2019, 9:11am UTC](https://discourse.processing.org/t/help-for-creating-own-library-add-java-file-to-sketch/7130/2 "2019-01-03T09:11:37Z")

</div>

> [@Wolfgang](#):
>
> i created a class called Adressbook. I copy the class inside the sketchbook.

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.

> [@Wolfgang](#):
>
> and entered import Adressbook;

1. Do not add the import statement because it is not needed

> [@Wolfgang](#):
>
> no library found for Addressbook — I got as error.

1. Theis error was created because of the import statement.

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [January 3, 2019, 2:06pm UTC](https://discourse.processing.org/t/help-for-creating-own-library-add-java-file-to-sketch/7130/3 "2019-01-03T14:06:38Z")

</div>

> [@Including shared pde files across projects](https://discourse.processing.org/t/including-shared-pde-files-across-projects/1701):
>
> Honey gave me an answer to my question [How can I make an include file for my scripts - SOLVED THANKS](https://discourse.processing.org/t/how-can-i-make-an-include-file-for-my-scripts-solved-thanks/1569) which to paraphrase said stick your variables in a file called file.pde in the same directory and it is included automagically. And that works nicely BUT I want to share my variables between projects, so I thought I could just move the file up a level to the projects directory and that might work, but no I get a message variable cannot be resolved. So I hunted about looking for an answer and…

---

<div class="post-metadata">

### Author: ![Wolfgang](https://avatars.discourse-cdn.com/v4/letter/w/54ee81/32.png) [@Wolfgang](https://discourse.processing.org/u/Wolfgang)
#### Post date: [January 3, 2019, 7:51pm UTC](https://discourse.processing.org/t/help-for-creating-own-library-add-java-file-to-sketch/7130/4 "2019-01-03T19:51:34Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

### Author: ![Wolfgang](https://avatars.discourse-cdn.com/v4/letter/w/54ee81/32.png) [@Wolfgang](https://discourse.processing.org/u/Wolfgang)
#### Post date: [January 3, 2019, 7:53pm UTC](https://discourse.processing.org/t/help-for-creating-own-library-add-java-file-to-sketch/7130/5 "2019-01-03T19:53:06Z")

</div>

All Contact should be Addressbook I know forgot to corrected.

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [January 4, 2019, 10:58am UTC](https://discourse.processing.org/t/help-for-creating-own-library-add-java-file-to-sketch/7130/6 "2019-01-04T10:58:31Z")

</div>

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

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [January 21, 2019, 5:46pm UTC](https://discourse.processing.org/t/help-for-creating-own-library-add-java-file-to-sketch/7130/7 "2019-01-21T17:46:03Z")

</div>

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.
