【发布时间】:2010-06-18 15:22:45
【问题描述】:
我希望能够从我的 AIR 应用程序中操作扫描仪。由于本机不支持此功能,因此我尝试使用 NativeProcess 类来启动可以运行扫描仪的 jar 文件。 Java 代码使用 JTwain 库来操作扫描仪。 Java 应用程序本身运行良好,AIR 应用程序可以启动并与 Java 应用程序通信。问题似乎是,每当我尝试使用 JTwain 中的函数(依赖于 JTwain.dll)时,如果 AIR STARTED IT,应用程序就会死掉。
我不确定从本机进程引用 dll 文件是否有一些限制或什么。我在下面包含了我的代码
Java 代码-
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 {
}
}
}
空气应用-
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"/>
我很想解释一下它为什么会死。我已经做了足够的测试,我绝对知道杀死它的行是 SourceManager.instance().getCurrentSource()。我会喜欢任何建议。谢谢。
【问题讨论】:
-
没有。当 Java 应用程序在 Flex 调用中终止时,似乎没有一个 catch 语句被命中。它只是默默地死去。
-
暂时把 DLL 放到你的 windows 系统文件夹中(确保你没有覆盖任何东西)然后尝试。这会告诉你是否有路径问题
-
你能在 stderr 上放一个监听器吗?并以这种方式获取堆栈跟踪?
-
我将 DLL 放在 Windows\System 文件夹中 - 没有区别。我使用以下方法为 stderr 添加了一个侦听器: nativeProcess.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, onStandardErrorData);仍然没有听到任何回音。不过还是谢谢。
-
@Donny 你试过把 DLL 放到 windows 系统目录了吗?
标签: java apache-flex process air native