Menu

#584 Consider Jython addToPath command which downloads jars

closed-fixed
nobody
None
5
2024-10-20
2017-07-19
No

It would be nice to have a command to download a jar file and put it on the path. Right now, one must download and unzip the jar file, and then add the unzip directory to the jython path.

 # cd /tmp/jcodec/
 # wget -N http://jcodec.org/downloads/jcodec-0.1.9.jar
 # jar xvf jcodec-0.1.9.jar
 import sys
 sys.path.append("/tmp/jcodec/")  

instead:

 addToSearchPath( sys.path, 'http://jcodec.org/downloads/jcodec-0.1.9.jar', monitor )

Discussion

  • Jeremy Faden

    Jeremy Faden - 2017-07-19

    See the cookbook entry about "Jython Search Path"

     
  • Jeremy Faden

    Jeremy Faden - 2017-07-27

    This should allow a zip containing a .jar to be accessed, for example:
    addToSearchPath('http://www.trieuvan.com/apache//commons/math/binaries/commons-math3-3.6.1-bin.zip/commons-math3-3.6.1/commons-math3-3.6.1.jar', monitor )

    This would contain the logic to extract the jar, if necessary unpack the jar, and then put it on the search path.

     
  • Jeremy Faden

    Jeremy Faden - 2017-07-28

    Note the code should work with any Autoplot:

    import sys
    addToSearchPath( sys.path, 'http://www-us.apache.org/dist//commons/math/binaries/commons-math3-3.6.1-bin.zip/commons-math3-3.6.1/commons-math3-3.6.1.jar', monitor )
    from org.apache.commons.math3.distribution import BetaDistribution
    beta= BetaDistribution(2,5)
    
    xx= linspace(0,1.0,100)
    yy= zeros(100)
    for i in indgen(100):
        yy[i]= beta.density(xx[i].value())
    #yy= map( xx, beta.density )
    plot( xx, yy )
    
     

    Last edit: Jeremy Faden 2018-03-05
  • Jeremy Faden

    Jeremy Faden - 2017-10-08

    I needed something to decode ico files:

    path= 'http://jfaden.net/~jbf/autoplot/lib/image4j-0.7.2.jar'
    from org.autoplot.datasource import FileSystemUtil
    jarFile= FileSystemUtil.doDownload( path, monitor )
    import sys
    sys.path.append( jarFile.toString() )
    
    ff= downloadResourceAsTempFile(URL('http://cottagesystems.com/favicon.ico'),monitor)
    
    from  net.sf.image4j.codec.ico import ICODecoder
    imgs= ICODecoder.read(ff)
    
    from javax.swing import JPanel,JLabel,ImageIcon,JOptionPane,BoxLayout
    list= JPanel()
    list.setLayout( BoxLayout(list,BoxLayout.Y_AXIS) )
    for i in imgs:
        l= JLabel('icon %dx%d' % ( i.getWidth(), i.getHeight() ) )
        print ImageIcon(i)
        l.setIcon( ImageIcon( i ) )
        list.add( l )
    list.revalidate()
    JOptionPane.showMessageDialog(None,list)
    
     
  • Jeremy Faden

    Jeremy Faden - 2017-11-26

    For some reason the above script works with a fresh Autoplot launch, but this script doesn't:

    #f1= getFile('http://central.maven.org/maven2/org/jcodec/jcodec/0.2.2/jcodec-0.2.2.jar',monitor.getSubtaskMonitor('load jar'))
    #f2= getFile('http://central.maven.org/maven2/org/jcodec/jcodec-javase/0.2.2/jcodec-javase-0.2.2.jar',monitor.getSubtaskMonitor('load BufferedImage support jar'))
    
    from org.autoplot.datasource import FileSystemUtil
    f1= FileSystemUtil.doDownload( 'http://central.maven.org/maven2/org/jcodec/jcodec/0.2.2/jcodec-0.2.2.jar',monitor.getSubtaskMonitor('load jar') )
    f2= FileSystemUtil.doDownload( 'http://central.maven.org/maven2/org/jcodec/jcodec-javase/0.2.2/jcodec-javase-0.2.2.jar',monitor.getSubtaskMonitor('load BI jar') )
    
    # on 2017-11-26, I had to unjar the jar file on Unix.
    import sys
    #sys.path.append(f1.toString())  
    sys.path.append(f2.toString())  
    #sys.path.append('/tmp/jcodec/jcodec')  
    #sys.path.append('/tmp/jcodec/jcodec-javase')  
    
    from org.jcodec.api.awt import AWTSequenceEncoder
    from org.jcodec.common.io import NIOUtils
    from javax.imageio import ImageIO
    from java.io import File
    
    from org.jcodec.common.model import Rational
    from org.jcodec.common.model import ColorSpace, Picture
    from org.jcodec.common.Format import MOV,H264
    
    enc = AWTSequenceEncoder.createSequenceEncoder(File("/tmp/filename.mp4"),24)
    dir(enc)
    dd= '/home/jbf/pngwalk/mms/'
    ff= listDirectory(dd+'product_*.png')
    for f in ff:
       img= ImageIO.read( File(dd+f) )
       enc.encodeImage(img)
    enc.finish()
    

    Actually... Sometimes the above example (2017-10-08) doesn't work. I start a fresh jumbojar version and it's not working.

     

    Last edit: Jeremy Faden 2017-11-26
  • Jeremy Faden

    Jeremy Faden - 2018-02-01

    I need to get this into the nightly tests. I was hoping this script would work:

    s='https://bitbucket.org/xerial/sqlite-jdbc/downloads/sqlite-jdbc-3.21.0.jar'
    
    import sys
    addToSearchPath( sys.path,s,monitor)
    
    from java.sql import DriverManager
    
    import org.sqlite.SQLiteConfig  # load the class
    org.sqlite.SQLiteConfig
    
    connection = DriverManager.getConnection("jdbc:sqlite:sample.db")
    
    
    statement = connection.createStatement()
    statement.setQueryTimeout(30)  # set timeout to 30 sec.
    
    statement.executeUpdate("drop table if exists person");
    statement.executeUpdate("create table person (id integer, name string)");
    statement.executeUpdate("insert into person values(1, 'leo')");
    statement.executeUpdate("insert into person values(2, 'yui')");
    
    rs = statement.executeQuery("select * from person");
    while(rs.next()):
        print "name = " + rs.getString("name")
        print "id = " + rs.getInt("id")
    
     
  • Jeremy Faden

    Jeremy Faden - 2018-03-05

    I've spent some time with this this morning. Some commented code, claiming to be unneeded was needed, and single-jar releases support this now.

    On webstart I get:
    java.security.AccessControlException: java.security.AccessControlException: access denied ("java.io.FilePermission" "/home/jbf/autoplot_data/jython/org/apache/commons/math3/random/Well19937c.class" "read")

    Here's the script I was using:

    import sys
    
    monitor.started()
    d= 'http://central.maven.org/maven2/org/jcodec/'
    addToSearchPath( sys.path,d+'jcodec/0.2.2/jcodec-0.2.2.jar',monitor.getSubtaskMonitor('load jar'))
    addToSearchPath( sys.path,d+'jcodec-javase/0.2.2/jcodec-javase-0.2.2.jar',monitor.getSubtaskMonitor('load jar'))
    
    from org.jcodec.api.awt import AWTSequenceEncoder
    from org.jcodec.common.io import NIOUtils
    from javax.imageio import ImageIO
    from java.io import File
    
    from org.jcodec.common.model import Rational
    from org.jcodec.common.model import ColorSpace, Picture
    from org.jcodec.common.Format import MOV,H264
    
    enc = AWTSequenceEncoder.createSequenceEncoder(File("/tmp/filename.mp4"),24)
    dir(enc)
    dd= '/home/jbf/pngwalk/mms/'
    ff= listDirectory(dd+'product_*.png')
    monitor.setTaskSize(len(ff))
    for f in ff:
       monitor.setProgressMessage( f )
       monitor.setTaskProgress( monitor.getTaskProgress() + 1 )
       img= ImageIO.read( File(dd+f) )
       enc.encodeImage(img)
    enc.finish()
    monitor.finished()
    
     

    Last edit: Jeremy Faden 2018-03-07
  • Jeremy Faden

    Jeremy Faden - 2018-11-28

    I used this successfully when getting materials ready for Kris' AGU talk.

     
  • Jeremy Faden

    Jeremy Faden - 2019-01-03
    • status: open --> closed-fixed
     
  • Jeremy Faden

    Jeremy Faden - 2024-10-20

    This does not work with github releases, like https://github.com/jamesmudd/jhdf/releases/download/v0.8.3/jhdf-0.8.3.jar:

    from java.io import File
    import sys
    
    jarfile= 'https://github.com/jamesmudd/jhdf/releases/download/v0.8.3/jhdf-0.8.3.jar'
    docfile= 'https://github.com/jamesmudd/jhdf/releases/download/v0.8.3/jhdf-0.8.3-javadoc.jar'
    addToSearchPath(sys.path,jarfile,docfile,monitor.getSubtaskMonitor('jhdf'))
    
     
  • Jeremy Faden

    Jeremy Faden - 2024-10-20

    I fixed the problem with github releases, using downloadFileToTempFile when the FileSystem approach does not work.

     
MongoDB Logo MongoDB