Java 从Flex调用jar文件命令
我正在尝试从AIR应用程序调用.jar文件。我们可以通过Java 从Flex调用jar文件命令,java,actionscript-3,apache-flex,Java,Actionscript 3,Apache Flex,我正在尝试从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"
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>
请在这方面帮助我。通常我们不能直接运行JAR文件,所以在这里我们通过命令提示符运行。然后找到cmd.exe路径,然后只需按照下面的代码执行即可。只有在运行MacOS然后执行shell脚本(.sh)时,它才适用于windows 确保您需要在应用程序描述符XML文件中添加
extendedDesktop
还要确保在环境变量中指定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>
=新向量。;
进程参数推送(“/c”)//注意这里
push(“java-jar xyz.jar”);
var nativeProcessStartupInfo:nativeProcessStartupInfo=new nativeProcessStartupInfo();
nativeProcessStartupInfo.arguments=processArgs;
nativeProcessStartupInfo.exe=cmdFile;
nativeProcessStartupInfo.workingDirectory=File.userDirectory;
进程=新的NativeProcess();
process.addEventListener(ProgressEvent.STANDARD_输出数据,onOutputData);
process.addEventListener(ProgressEvent.STANDARD_ERROR_数据,onErrorData);
process.addEventListener(nativeProcessExiteEvent.EXIT,onExit);
process.addEventListener(IOErrorEvent.STANDARD_OUTPUT_IO_ERROR,onIOError);
process.addEventListener(IOErrorEvent.STANDARD_ERROR_IO_ERROR,onIOError);
进程启动(nativeProcessStartupInfo);
}
公共函数onOutputData(事件:ProgressEvent):无效
{
跟踪(“get:,process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable));
}
公共函数onErrorData(事件:ProgressEvent):无效
{
跟踪(“错误-”,process.standardError.readUTFBytes(process.standardError.bytesAvailable));
}
公共函数onExit(事件:NativeProcessExiteEvent):无效
{
跟踪(“进程已退出”,事件.exitCode);
}
公共函数错误(事件:IOErrorEvent):无效
{
跟踪(event.toString());
}
]]>
jaganath,谢谢你的回复。它很有效,但就我所知,我想知道你为什么使用processArgs.push(“/c”)作为参数?
<?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>