Skip to content

Commit a707ae5

Browse files
jaylettojwb
authored andcommitted
Provide working example commands for Java.
This is slightly harder than for other languages, because of the way that classpaths and JNI work; and because there isn't a standard place to put third-party libraries. So now we recommend that Java folks copy the JNI and jar files into this repository's root, set their CLASSPATH environment variable, and then run simple commands that should work on that basis. Our example code is now in the "code.java" package, because that's its layout on disk; otherwise we have to mess with the classpath even more to get things to run properly.
1 parent 4408d95 commit a707ae5

File tree

8 files changed

+48
-1
lines changed

8 files changed

+48
-1
lines changed

code/java/delete1.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package code.java;
2+
13
import org.xapian.WritableDatabase;
24
import org.xapian.XapianConstants;
35

code/java/index1.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package code.java;
2+
13
import java.io.File;
24
import java.io.FileNotFoundException;
35
import java.util.Scanner;
@@ -11,6 +13,8 @@
1113
import org.xapian.XapianConstants;
1214
import org.xapian.XapianJNI;
1315

16+
import code.java.support;
17+
1418
public class index1 {
1519

1620
// Command line args - datapath dbpath

code/java/index1.java.data=2f100-objects-v1.csv_db.out

Whitespace-only changes.

code/java/search1.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package code.java;
2+
13
import org.xapian.Database;
24
import org.xapian.Document;
35
import org.xapian.Enquire;

code/java/support.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/* Support code for Java examples */
2+
package code.java;
3+
24
import java.util.ArrayList;
35

46
public class support {

conf.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ def xapian_code_example_command(ex):
325325
return "cli-csc -unsafe -target:exe -out:%s.exe %s -r:XapianSharp.dll\n./%s.exe" \
326326
% (ex, xapian_code_example_filename(ex), ex)
327327
elif highlight_language == 'java':
328-
return "javac %s\njava %s" \
328+
return "javac %s\njava code.java.%s" \
329329
% (xapian_code_example_filename(ex), ex)
330330
else:
331331
print "Unhandled highlight_language '%s'" % highlight_language
@@ -555,8 +555,20 @@ def run(self):
555555
directives.register_directive('xapiancodesnippet', XapianCodeSnippet)
556556

557557
class XapianInclude(Include):
558+
559+
option_spec = {
560+
'optional': directives.flag,
561+
}
562+
option_spec.update(Include.option_spec)
563+
558564
def run(self):
559565
self.arguments[0] = re.sub(r'\bLANGUAGE\b', highlight_language, self.arguments[0])
566+
if 'optional' in self.options:
567+
# We want to silently fail if we can't find the file.
568+
# Don't bother to process this 'properly', because we should
569+
# only use it with straightforward paths.
570+
if not os.path.exists(directives.path(self.arguments[0])):
571+
return []
560572
return super(XapianInclude, self).run()
561573

562574
# Usage:
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Since there isn't a standard location to install third-party Java
2+
libraries, you will likely have to set the ``CLASSPATH`` variable
3+
appropriately to indicate that you wish to use the Xapian Java
4+
bindings.
5+
6+
There are two parts to the bindings: a jarfile (``xapian.jar``)
7+
containing the Java classes, and the JNI library (such as
8+
``libxapian_jni.so`` on Linux, or ``libxapian_jni.jnilib`` on macOS)
9+
that connects them to Xapian itself. The easiest way to get this
10+
working is to copy those two files to the top-level directory of this
11+
repository. If you built your own Java bindings, the files will be in
12+
``java/built`` in the bindings source code. Then you can use the
13+
following classpath (if on Linux)::
14+
15+
xapian.jar:libxapian_jni.so:.
16+
17+
If you set the ``CLASSPATH`` variable to this, then the example
18+
commands will work as shown. For instance, if you're using the
19+
``bash`` shell, you should run the following before any example
20+
commands (again, on Linux)::
21+
22+
export CLASSPATH=xapian.jar:libxapian_jni.so:.

overview.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ examples, we provide the commands needed to compile (if necessary) and run
123123
the code described. These commands are intended to be run from the top-level
124124
directory of the source for this guide.
125125

126+
.. xapianinclude:: language_specific/LANGUAGE/running_examples.rst
127+
:optional:
128+
126129
.. todo:: link to here from every howto and everything that needs the data files and example code
127130

128131
Contributing

0 commit comments

Comments
 (0)