From: Ole Streicher <olebole@debian.org>
Date: Thu, 16 Mar 2017 09:27:43 +0100
Subject: Package jystilts

---
 src/docs/sun256.xml                                | 56 ++++------------------
 src/main/uk/ac/starlink/ttools/build/JyStilts.java | 33 +++++++++++--
 2 files changed, 38 insertions(+), 51 deletions(-)

diff --git a/src/docs/sun256.xml b/src/docs/sun256.xml
index f34e510..a89fabc 100644
--- a/src/docs/sun256.xml
+++ b/src/docs/sun256.xml
@@ -1481,53 +1481,15 @@ is given in the following subsections.
 <subsect id="jyrun">
 <subhead><title>Running JyStilts</title></subhead>
 
-<p>The easiest way to run JyStilts is to download the standalone
-<code>jystilts.jar</code> file from the STILTS web page,
-and simply run
-<verbatim>
-java -jar jystilts.jar   
-</verbatim>
-This file includes jython itself and all the STILTS and JyStilts classes.
-To use the JyStilts commands, you will need to import the stilts
-module using a line like "<code>import stilts</code>"
-from Jython in the usual Python way.
-</p>
-
-<p>Alternatively, you can run JyStilts from an existing Jython installation
-using just the <code>stilts.jar</code> file.
-First, make sure that Jython is installed;
-it is available from <webref url="http://www.jython.org/"/>,
-and comes as a self-installing jar file.
-JyStilts has been tested, and appears to work, on versions
-2.5.0 and 2.5.1; it's recommended to use the latest version
-if you don't have some reason to use one of the others.
-Some earlier versions of JyStilts worked with jython 2.2.1, 
-but that no longer seems to be the case; it might be possible
-to reinstate this if there is some pressing need.
-</p>
-
-<p>To use JyStilts, you then just need to
-start jython with the <code>stilts.jar</code> file on your classpath,
-for instance like this:
-<verbatim>
-jython -J-classpath /some/where/stilts.jar
-</verbatim>
-or (C-shell):
-<verbatim>
-setenv CLASSPATH /some/where/stilts.jar
-jython
-</verbatim>
-</p>
-
-<p>Optionally, you can extract the <code>stilts.py</code> module 
-from the stilts.jar file 
-(using a command like "<code>unzip stilts.jar stilts.py</code>")
-and put it in a directory on your jython 
-<code>sys.path</code> (e.g. <code>jythondir/Lib</code>);
-this may cause jython to compile it to bytecode (<code>stilts$py.class</code>)
-and thus improve startup time.
-Note that in this case you will still need the <code>stilts.jar</code> 
-file on your classpath as above.
+<p>You can run JyStilts from an existing Jython installation.
+Just make sure that the
+<webref url="https://packages.debian.org/jython-stils">
+<code>jython-stilts</code></webref> Debian package is installed.
+Then you can just do an
+<verbatim><![CDATA[
+>>> import stilts
+]]></verbatim>
+to have the stilts package available.
 </p>
 
 </subsect>
diff --git a/src/main/uk/ac/starlink/ttools/build/JyStilts.java b/src/main/uk/ac/starlink/ttools/build/JyStilts.java
index 8f7b422..1fcb13f 100644
--- a/src/main/uk/ac/starlink/ttools/build/JyStilts.java
+++ b/src/main/uk/ac/starlink/ttools/build/JyStilts.java
@@ -168,9 +168,36 @@ public class JyStilts {
             "for tutorial and full usage information.",
             "'''",
             "",
-            "from __future__ import generators",
             "__author__ = 'Mark Taylor'",
             "",
+	    "import sys",
+	    "import java.io",
+	    "import java.nio.file",
+	    "import java.util.jar",
+	    "",
+            "# see also http://bugs.jython.org/issue547727",
+            "def _recursivelyAddJar(jarfile):",
+            "    if jarfile.canonicalPath in sys.path:",
+            "        return",
+            "    sys.path.append(jarfile.canonicalPath)",
+            "    try:",
+            "        jar = java.util.jar.JarFile(jarfile)",
+            "        if jar and jar.manifest:",
+            "            jarAttrs = jar.manifest.mainAttributes",
+            "            jarClassPath = jarAttrs.get(java.util.jar.Attributes.Name.CLASS_PATH)",
+            "            if jarClassPath:",
+            "                for p in jarClassPath.split():",
+            "                    _recursivelyAddJar(java.io.File(jarfile.parent, p))",
+            "    except java.io.FileNotFoundException:",
+            "        pass # ignore jars that are not there",
+            "    except java.nio.file.NoSuchFileException:",
+            "        pass # ignore jars that are not there",
+            "",
+            "_recursivelyAddJar(java.io.File('/usr/share/java/starlink-ttools.jar'))",
+            "",
+            "del _recursivelyAddJar",
+            "del sys",
+            "",
         };
     }
 
@@ -318,9 +345,7 @@ public class JyStilts {
             /* Utility to raise an error if a handler can't write multiple
              * tables. */
             "def _check_multi_handler(handler):",
-            "    if not " + getImportName( Class.class )
-                          + ".forName('" + MultiStarTableWriter.class.getName()
-                                         + "')"
+            "    if not " + "_MultiStarTableWriter"
                           + ".isInstance(handler):",
             "        raise TypeError('Handler %s cannot write multiple tables' "
                                     + "% handler.getFormatName())",
