2014-01-23 67 views
0

我試圖從AIR app中調用.jar文件。我們可以通過NativeProcess執行此操作。我的代碼是未來從Flex中調用jar文件命令

<?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. 

PLZ幫我在這。

回答

2

通常我們不能直接運行JAR文件,所以我們在這裏通過命令提示符運行。然後找到cmd.exe路徑,然後只是跟隨下面的代碼它工作正常。它只適用於Windows需要運行MacOS然後執行shell腳本(.sh)的情況。

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

還要確保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> 
+0

jaganath,謝謝你的答覆。它工作的格柵,但就我的知識而言,我想知道爲什麼使用processArgs.push(「/ c」)作爲參數? – shashi