gpt4 book ai didi

java - 从 Flex 调用 jar 文件命令

转载 作者:行者123 更新时间:2023-12-02 06:17:42 26 4
gpt4 key购买 nike

我正在尝试从 AIR 应用程序调用 .jar 文件。我们可以通过 NativeProcess 来执行此操作。接下来是我的代码

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="init()">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
var process:NativeProcess;
private function init():void
{
if (NativeProcess.isSupported)
{
Alert.show("suport native process.");
setupAndLaunch();
}
}
private function setupAndLaunch():void
{
var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
var file:File = File.userDirectory.resolvePath("xyz.jar");

nativeProcessStartupInfo.executable = file;

process = new NativeProcess();
process.start(nativeProcessStartupInfo);
process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);
process.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, onErrorData);
process.addEventListener(NativeProcessExitEvent.EXIT, onExit);
process.addEventListener(IOErrorEvent.STANDARD_OUTPUT_IO_ERROR, onIOError);
process.addEventListener(IOErrorEvent.STANDARD_ERROR_IO_ERROR, onIOError);
}

public function onOutputData(event:ProgressEvent):void
{
trace("Got: ", process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable));
}

public function onErrorData(event:ProgressEvent):void
{
trace("ERROR -", process.standardError.readUTFBytes(process.standardError.bytesAvailable));
}

public function onExit(event:NativeProcessExitEvent):void
{
trace("Process exited with ", event.exitCode);
}

public function onIOError(event:IOErrorEvent):void
{
trace(event.toString());
}
]]>
</fx:Script>
</s:WindowedApplication>

在调试时,它抛出以下错误

Error: Error #3219: The NativeProcess could not be started. '%1 is not a valid Win32 application.

请帮助我。

最佳答案

通常我们不能直接运行JAR文件,所以这里我们通过命令提示符运行。然后找到cmd.exe路径,然后按照下面的代码操作就可以了。仅当要运行 MacOS 然后执行 shell 脚本(.sh) 时,它才适用于 Windows。

确保您需要添加<supportedProfiles>extendedDesktop desktop</supportedProfiles>在应用程序描述符 XML 文件中。

还要确保在环境变量中分配了 java 类路径。

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="init()">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
private var process:NativeProcess;
private function init():void
{
if (NativeProcess.isSupported)
{
Alert.show("suport native process.");
setupAndLaunch();
}
}
private function setupAndLaunch():void
{
var cmdFile:File = new File("c:\\Windows\\System32\\cmd.exe");

var processArgs:Vector.<String> = new Vector.<String>;  
processArgs.push("/c"); //Note here
processArgs.push("java -jar xyz.jar");

var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
nativeProcessStartupInfo.arguments = processArgs;
nativeProcessStartupInfo.executable = cmdFile;
nativeProcessStartupInfo.workingDirectory = File.userDirectory;

process = new NativeProcess();
process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);
process.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, onErrorData);
process.addEventListener(NativeProcessExitEvent.EXIT, onExit);
process.addEventListener(IOErrorEvent.STANDARD_OUTPUT_IO_ERROR, onIOError);
process.addEventListener(IOErrorEvent.STANDARD_ERROR_IO_ERROR, onIOError);

process.start(nativeProcessStartupInfo);
}

public function onOutputData(event:ProgressEvent):void
{
trace("Got: ", process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable));
}

public function onErrorData(event:ProgressEvent):void
{
trace("ERROR -", process.standardError.readUTFBytes(process.standardError.bytesAvailable));
}

public function onExit(event:NativeProcessExitEvent):void
{
trace("Process exited with ", event.exitCode);
}

public function onIOError(event:IOErrorEvent):void
{
trace(event.toString());
}
]]>
</fx:Script>
</s:WindowedApplication>

关于java - 从 Flex 调用 jar 文件命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21304788/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com