How to execute jar file in python multithreading
Question: In my project, I have a jar file (which was written by other developer) to
copy content from a pdf to text file. Using python multi threading concept, I
tried to execute this jar.

After I ran this script, I can able to see the text files are created. but the
file size is 0KB. Why the contents are not copied to this file. However I
tried running this jar in command line, this works as expected. Can someone
tell please provide the solution?

    
    
    from threading import Thread
    import os
    import sys
    import time
    import urllib2
    from lxml import etree, html
    import re
    import Queue
    import traceback
    
    
    def createfile(x):
        try:
            file="test_"+str(x)
            print "java -jar tika-app-1.1.jar -t --encoding=utf8 \"%s\" > \"%s\" "%("C:\\samplefile.pdf",file)
            os.system("java -jar tika-app-1.1.jar -t --encoding=utf8 \"%s\" > \"%s\" "%("C:\tmp\samplefile.pdf",file))
        except Exception,e:
            print "excet",traceback.format_exc()
    
    def process():
        try:
            result = Queue.Queue()
            threads = [Thread(target=createfile, args=(x,)) for x in range(1,5)]
            for t in threads:
                t.start()
            for t in threads:
                t.join()
        except:
            print "exception",traceback.format_exc()
            pass
        end_time = time.time()
        print "Estimate time", end_time - start_time
    
    if __name__ == '__main__':
        process()
    

My output:

    
    
    Exception in thread "main" java.net.MalformedURLException: unknown protocol: c
            at java.net.URL.<init>(Unknown Source)
            at java.net.URL.<init>(Unknown Source)
            at java.net.URL.<init>(Unknown Source)
            at org.apache.tika.cli.TikaCLI.process(TikaCLI.java:393)
            at org.apache.tika.cli.TikaCLI.main(TikaCLI.java:101)
    Exception in thread "main" java.net.MalformedURLException: unknown protocol: c
            at java.net.URL.<init>(Unknown Source)
            at java.net.URL.<init>(Unknown Source)
            at java.net.URL.<init>(Unknown Source)
            at org.apache.tika.cli.TikaCLI.process(TikaCLI.java:393)
            at org.apache.tika.cli.TikaCLI.main(TikaCLI.java:101)
    Exception in thread "main" java.net.MalformedURLException: unknown protocol: c
            at java.net.URL.<init>(Unknown Source)
            at java.net.URL.<init>(Unknown Source)
            at java.net.URL.<init>(Unknown Source)
            at org.apache.tika.cli.TikaCLI.process(TikaCLI.java:393)
            at org.apache.tika.cli.TikaCLI.main(TikaCLI.java:101)
    Exception in thread "main" java.net.MalformedURLException: unknown protocol: c
            at java.net.URL.<init>(Unknown Source)
            at java.net.URL.<init>(Unknown Source)
            at java.net.URL.<init>(Unknown Source)
            at org.apache.tika.cli.TikaCLI.process(TikaCLI.java:393)
            at org.apache.tika.cli.TikaCLI.main(TikaCLI.java:101)
    Estimate time 1.73799991608
    


Answer: You tell the Java application to read this file: `C: mpsamplefile.pdf` because
`\t` is turned into a Tab characters in a Python string. The Java application
then sees `C:` without a `/` or `\` after it and assumes this must be a URL
(like `http:` or `ftp:`). But when it asks around, no URL protocol handler
supports it, hence the exception.

To avoid problems like this one, use `os.path.join()`:

    
    
    inputFile = os.path.join('C:', 'tmp', 'samplefile.pdf')
    

Or use `/` instead of `\`; Java on Windows will convert these delimiters when
accessing files.

