Donny
Donny

Reputation: 89

Running a Java program with a .dll from Adobe AIR's native process

I would like to be able to operate a scanner from my AIR application. Since there's no support for this natively, I'm trying to use the NativeProcess class to start a jar file that can run the scanner. The Java code is using the JTwain library to operate the scanner. The Java application runs fine by itself, and the AIR application can start and communicate with the Java application. The problem seems to be that any time I attempt to use a function from JTwain (which relies on the JTwain.dll), the application dies IF AIR STARTED IT.

I'm not sure if there's some limit about referencing dll files from the native process or what. I've included my code below

Java code-

    while(true)
    {
        try {
            System.out.println("Start");
            text = in.readLine();
            Source source = SourceManager.instance().getCurrentSource();
            System.out.println("Java says: "+ text);
        }
        catch (IOException e)
        {
            System.err.println("Exception while reading the input. " + e);
        }
        catch (Exception e) {
            System.out.println("Other exception occured: " + e.toString());
        }
        finally {
        }
    }   
}

Air application-

        import mx.events.FlexEvent;

        private var nativeProcess:NativeProcess;
        private var npInfo:NativeProcessStartupInfo;
        private var processBuffer:ByteArray;
        private var bLength:int = 0;

        protected function windowedapplication1_applicationCompleteHandler(event:FlexEvent):void
        {
            var arg:Vector.<String> = new Vector.<String>;
            arg.push("-jar");
            arg.push(File.applicationDirectory.resolvePath("Hello2.jar").nativePath);

            processBuffer = new ByteArray;

            npInfo = new NativeProcessStartupInfo;
            npInfo.executable = new File("C:/Program Files/Java/jre6/bin/javaw.exe");
            npInfo.arguments = arg;

            nativeProcess = new NativeProcess;
            nativeProcess.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onStandardOutputData);
            nativeProcess.start(npInfo);
        }

        private function onStandardOutputData(e:ProgressEvent):void
        {
            tArea.text += nativeProcess.standardOutput.readUTFBytes(nativeProcess.standardOutput.bytesAvailable);
        }


        protected function button1_clickHandler(event:MouseEvent):void
        {
            tArea.text += 'AIR app: '+tInput.text + '\n';
            nativeProcess.standardInput.writeMultiByte(tInput.text + "\n", 'utf-8');
            tInput.text = '';
        }


        protected function windowedapplication1_closeHandler(event:Event):void
        {
            nativeProcess.closeInput();
        }

    ]]>
</fx:Script>
<s:Button label="Send" x="221" y="11" click="button1_clickHandler(event)"/>
<s:TextInput id="tInput" x="10" y="10" width="203"/>
<s:TextArea id="tArea" x="10" width="282" height="88" top="40"/>

I would love some explanation about why this is dying. I've done enough testing that I know absolutely that the line that kills it is the SourceManager.instance().getCurrentSource(). I would love any suggestions. Thanks.

Upvotes: 2

Views: 2687

Answers (2)

Romain Hippeau
Romain Hippeau

Reputation: 24375

When calling Java add this -Djava.library.path=location_of_dll to the command line

Upvotes: 1

Lauri Lehtinen
Lauri Lehtinen

Reputation: 10857

I have 0 experience with Air, but this reminded me of a Java issue I once spent some time figuring out. I don't have a suggestion on why the scanning doesn't work, but I think a stack trace would be your best friend right now.

I'm guessing you're relying on this line to capture and display it?

nativeProcess.standardOutput.readUTFBytes(nativeProcess.standardOutput.bytesAvailable);

However, you are writing IOExceptions to System.err - is there a nativeProcess.standardError you could read in Air? Alternatively, output everything to System.out.

Upvotes: 0

Related Questions