Installing a java library. Twitter4j, in this case.

_vk_vk
edited August 2014 in Library Questions

Just want to check if I did the right thing. I followed wiki's instructions on How_to_Install_a_Contributed_Library. And the library is working.

My doubts are:

1- Am I really supposed to rename the library .jar? And only one of them?

The library folder have a tree like

-twitter4j-4.0.2ƒ

-- several folders in this level

--lib

---twitter4j-core-4.0.2.jar

---twitter4j-examples-4.0.2.jar

---twitter4j-media-support-4.0.2.jar

---twitter4j-stream-4.0.2.jar

---some others like...

What I did was to rename only one of them (core) to the same name as the folder I created (with no '-' or . in it) so it became:

-twitter4j402ƒ

--library

---twitter4j402.jar >> RENAMED

---all other jars with original names

2- This is working fine. Is this how it is supposed to be done?

thanks.

Answers

  • Answer ✓

    OK, at least you followed the instructions and it worked. How does it work? Magic, of course!

    Seriously, that's a good question.

    BTW, I guess there is a typo in your message. I suppose your renamed jar has a f at the end, or the folder doesn't have it, right?

    OK, I haven't dug in the Processing code for this loading mechanism, but I can emit some guesses, based on experience and on what I know of Java...

    The Java compiler is quite nearsighted: it only knows the Java source code file it is parsing, the .class files found in the same folder than the destination of the current .class file, and, of course, the standard Java packages of the JDK. By default, it doesn't know where to find other .class files or .jar files (collections of .class files grouped in a Zip file).

    Fortunately, there are mechanisms to tell the compiler where to search other packages.
    The first one is the classpath, a delicate topic covered by lot of articles on the Net...

    In short, the classpath lists the locations where .class files can be found. It is a list of directories. Three cases:

    • The directory is just a folder with a bunch of .class files without package declaration.
    • The directory is the root of folders with .class files in each of them, following the package declarations. Ie. a class in a package a.b is expected to be found in the rootFolder/a/b folder.
    • The directory is actually a jar file (or a Zip file) with similar structure as above, put in a compressed archive.

    Note that if you specify a classpath, Java no longer consider the current folder as part of it, you have to add . as an entry of the classpath...

    Another mechanism I have seen widely used is to use (abuse!) the java.ext.dirs property to specify directories where a bunch of jar files can be found. Searching for clarifying / refreshing my knowledge shown this mechanism is no longer needed since Java 6, as we can now specify the * wildcard to a classpath directory, for the same usage.

    Back to our topic, let's guess why Processing requires us to have a fixed directory structure, with well defined name schemes... I will write affirmatively, but as said, these are guesses, so I can be wrong on some points...

    So, when running a sketch, Processing inspects the code in search of import statements. If found, and they don't belong to standard java / javax packages, it searches for libraries. (Perhaps it does that unconditionally, but it would be slower.)
    So, when a library is used, Processing lists the folders found in <sketchbook folder>/libraries For each folder found there, it goes in the library folder (not 'lib') and searches for a jar of same name than the library (with the .jar extension, of course), to include it in the class path.

    Now, I am not sure for the other jar files... Perhaps Processing uses the wildcard notation, perhaps it uses the java.ext.dirs mechanism, perhaps it uses some Java class loading API (probably) to load everything in the same folder. In this case, the need to rename the main jar to the name of the library might only serve for checking the validity of a library?

    HTH.

  • Thanks PhiLho. The thing is that the wiki says nothing about the others .jar files.

    When I choose to import the library using the menu, the IDE does write imports for all .jars in the foldes. That's cool.

    import twitter4j.util.*;
    import twitter4j.*;
    import twitter4j.management.*;
    import twitter4j.api.*;
    import twitter4j.conf.*;
    import twitter4j.json.*;
    import twitter4j.auth.*;
    
Sign In or Register to comment.